Class Cast Exception while reading the int value from a session object
Kumara Swamy
Ranch Hand
Joined: Nov 17, 2008
Posts: 34
posted
0
Hi,
I am reading the int value from a session object in a jsp as follows.
session.setAttribute("number", 3);
accessing the above value as follows:
int n = Integer.parseInt((String)session.getAttribute("number"));
in the above case i am getting a Class Cast Exception.
if i read the same value by using the following code it's fine.
int n = Integer.parseInt(session.getAttribute("number").toString());
I have written this code long back it was working fine but from today only its getting the exception. I didnt update the system/tomcat server configurations.
Please help me out to resolve the issue.
Vivek Kr Singh
Ranch Hand
Joined: Oct 12, 2007
Posts: 56
posted
0
If using 1.5 then
EDIT : done for missing brackets
SCJP 1.4
Kumara Swamy
Ranch Hand
Joined: Nov 17, 2008
Posts: 34
posted
0
Vivek Kr Singh wrote:If using 1.5 then
EDIT : done for missing brackets
Hi thanks for the reply,
My question is something different the problem with the following statement:
(String)session.getAttribute("number")
Or to put it another way an attribute is not a parameter.
Charbel Keyrouz
Ranch Hand
Joined: Jun 10, 2005
Posts: 46
posted
0
In fact you have set it like this:
session.setAttribute("number",3);
3 is not Integer it is int and this is the reason of the problem.
you should set it like this:
session.setAttribute("number","3") and read it (String) session.getAttribute("number");
or
session.setAttribute("number",new Integer(3)) and read it (Integer) session.getAttribute("number");