We help IT Professionals succeed at work.
Get Started

trouble casting session variable

Mark asked
Last Modified: 2012-05-09
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"));

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

Watch Question
This problem has been solved!
Unlock 2 Answers and 7 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE