Link to home
Start Free TrialLog in
Avatar of barnarp
barnarp

asked on

ClassCastException

Hi,

I keep getting a java.lang.ClassCastException exception on the following page and don't know why.

<jsp:useBean id="db" scope="session" class="dbpackage.dbbean" />
<%
db.LogoffFromDb();
session.invalidate();
response.sendRedirect("maxlogin.jsp");
%>

Bean code:

public void LogoffFromDb () throws Exception
{
if (GlobalConn != null) {
      try {
         GlobalConn.close();
         }
      catch (Exception ignored) {}
      }

}

Regards
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

>> <jsp:useBean id="db" scope="session" class="dbpackage.dbbean" />

I'm guessing this is causing it...

is there a class dbpackage.dbbean?

is it a bean?

Can I also suggest (as a sideline) that classes should start with a Capital Letter, and methods with a small letter...

That tends to be the norm, and makes your code much more easily readable and understandable :-)

Tim
Avatar of barnarp
barnarp

ASKER

Thanks,

yes there is such a class which I also use in other pages, and it is a bean.
Can you put:

<%
   Object o = session.getAttribute( "db" ) ;
   if( o != null )  
       out.println( db.getClass().getName() ) ;
%>

to see what the attribute in the session actually is?

I assume you do a similar bit of code on other pages...  can you compare them to this?

What's the difference?
Avatar of barnarp

ASKER

Found the problem.

db was also the name of another session variable which was type String.

That's why the cast error.

Thanks for the help
that would do it ;-)

glad you got it sorted :-)

Tim
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial