• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String is not an Integer .
 
Kumara Swamy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You set it as an Integer, why cast it to a String?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or to put it another way an attribute is not a parameter.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic