| Author |
casting int from session variable
|
Timothy Sam
Ranch Hand
Joined: Sep 18, 2005
Posts: 746
|
|
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 ]
|
SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
|
|
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 ]
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
 |
|
|
subject: casting int from session variable
|
|
|