• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Cast Exception while reading the int value from a session object

 
Kumara Swamy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If using 1.5 then

EDIT : done for missing brackets
 
Kumara Swamy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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")

here we are getting the Class Cast exception
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String is not an Integer .
 
Kumara Swamy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String is not an Integer .


But it was worked fine earlier its throwing the exception from today only. Its still working fine with some other servers.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumara Swamy wrote:
But it was worked fine earlier its throwing the exception from today only. Its still working fine with some other servers.


then can you find out the object type of the "number" attribute?
 
Kumara Swamy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is why its working fine with some servers only?


session.setAttribute("number", 3);

accessing the above value as follows:
(String)session.getAttribute("number");

its throwing the Class Cast exception on some systems only all the servers on those systems are same.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumara Swamy wrote:
its throwing the Class Cast exception on some systems only all the servers on those systems are same.


The answare is same . "String is not an Integer" . some other server may be doing magic
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You set it as an Integer, why cast it to a String?
 
Benjamin Hiner
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or to put it another way an attribute is not a parameter.
 
Charbel Keyrouz
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charbel Keyrouz wrote:
session.setAttribute("number",3);
3 is not Integer it is int and this is the reason of the problem.

This is not a problem. In JDK 1.5 and beyond the 3 will be autoboxed as an Integer. Otherwise, it would not even compile.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic