wood burning stoves*
The moose likes JSP and the fly likes Class Cast Exception while reading the int value from a session object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Class Cast Exception while reading the int value from a session object" Watch "Class Cast Exception while reading the int value from a session object" New topic
Author

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

Kumara Swamy
Ranch Hand

Joined: Nov 17, 2008
Posts: 34

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
If using 1.5 then

EDIT : done for missing brackets


SCJP 1.4
Kumara Swamy
Ranch Hand

Joined: Nov 17, 2008
Posts: 34
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

Joined: Jan 28, 2008
Posts: 5575

String is not an Integer .
Kumara Swamy
Ranch Hand

Joined: Nov 17, 2008
Posts: 34
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 17, 2008
Posts: 34
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 10, 2002
Posts: 60789
    
  65

You set it as an Integer, why cast it to a String?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Benjamin Hiner
Greenhorn

Joined: Feb 27, 2009
Posts: 12
Or to put it another way an attribute is not a parameter.
Charbel Keyrouz
Ranch Hand

Joined: Jun 10, 2005
Posts: 46
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

Joined: Jan 10, 2002
Posts: 60789
    
  65

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.
 
Don't get me started about those stupid light bulbs.
 
subject: Class Cast Exception while reading the int value from a session object
 
Similar Threads
struts question
struts question
Java 7 Int to String casting
overriding equals method in my class
Servlet Download Problem