We help IT Professionals succeed at work.

trouble casting session variable

Mark
Mark asked
on
Maybe I just need another pair of eyes on this. I have the code:

int court_id = Integer.parseInt((String) session.getAttribute("courtId"));
int userType = Integer.parseInt((String) session.getAttribute("userType"));
int productType = Integer.parseInt((String) session.getAttribute("productType"));
%>
<body>[<%=session.getAttribute("productType")%>]</body></html>
<%

The 1st two assigments work fine, the 3rd gives me the error shown below. If I comment out the productType assignment, the <%= displays "[1]", so I know the session variable is set to a number. If I change the attribute string in the assignment from "productType" to "userType", it runs.

What am I doing wrong?
org.apache.jasper.JasperException: An exception occurred processing JSP page /updCourtProfile.jsp at line 40

37: int userType = Integer.parseInt((String) session.getAttribute("userType"));
38: int productType = Integer.parseInt((String) session.getAttribute("productType"));
39: %>
40: <body>[<%=session.getAttribute("productType")%>]</body></html>
41: <%
42: if (1==1) return;
43: String userName = null;

root cause 

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

Open in new window

Comment
Watch Question

Commented:
>java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String  
productType  must be an  Integer  type object.  
Therefore  
>int productType = Integer.parseInt((String) session.getAttribute("productType"));  
should  be  
int productType = ((Integer)session.getAttribute("productType")).intValue();
This line
><%=session.getAttribute("productType")%>  
works because the jsp expression takes care of the casting.
This should also  work  
${productType}
because the casting done by EL.
It's one of the most common mistakes.
When you have
session.setAttribute("someID", object of SomeType);
you always have to read in this way
SomeType object = (SomeType)session.getAttribute("someID");
It's different from request.getParameter("someID") - it always returns String.
I think that is the posible tha the attribute userType or productType cannot be cast to Integer,
try do a test with predefined values; someting like this:

session.setAttribute("userType","1");
session.setAttribute("productType","1");

int userType = Integer.parseInt((String) session.getAttribute("userType"));
int productType = Integer.parseInt((String) session.getAttribute("productType"));

Author

Commented:
Valeri, your suggested code did work, but I'm confused. I now have these two lines:

int userType = Integer.parseInt((String) session.getAttribute("userType"));
int productType = ((Integer)session.getAttribute("productType")).intValue();

The 1st syntax works too, but if I use that syntax on the 2nd line I get the error I described about. Any idea why I can do the parseInt() in the 1st line, but not in the second? Just lucky?
In which way you have set the attribute "productType"? In this way session.setAttribute("productType","1);? That's way you have to read it like in the first syntax.
The second will work if you have set it in this way session.setAttribute("productType", new Integer(1));
I dont't know how you have set the attribute  "productType"?!
Probably it will help you :
http://java.sun.com/docs/books/tutorial/java/generics/generics.html

Commented:
Please show us how you set the productType attribute in the first place.

Author

Commented:
ah ha! That's it. Here are the settings:

session.setAttribute("userType", rs.getString("user_type_cd"));

int productType = rs.getInt("product_type");
session.setAttribute("productType", productType);

I set the userType from a string and the productType from an int. I had no idea that mattered. For whatever reason I thought session attributes were typeless and just depended on the getAttribute() casting!

little did I know. Thanks. That solves the mystery.