• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting int from session variable

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have this statement in my Servlet

session.setAttribute("errorflag", 0);

I'm having problem casting it to int like...

int errorFlag = (int) session.getAttribute("errorflag", 0);

any idea on how to solve this? Thanks!
[ October 27, 2005: Message edited by: Timothy Sam ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
session.setAttribute("errorflag", 0);


This will not compile under Java 1.4. The second parameter must be an object. If you are using Java 1.5, this may be ok since the 0 may auto-box to an Integer. I'm not sure since I'm still using 1.4.

int errorFlag = (int) session.getAttribute("errorflag", 0);


First, getAttribute() does not take a second parameter.

Second, since you cannot cast anything to something that it is not, you cannot cast the return value of type Object to int, since int is not an Object. If an Integer is being returned, you could use the intValue() method to obtain the int value of the Integer.

Again, I'm not certain what the auto-boxing behavior of Java 1.5 would be -- it could be that your only issue is the spurious 2nd parameter.
[ October 27, 2005: Message edited by: Bear Bibeault ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic