• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wrapper Class Problem with the httpSession  RSS feed

 
Ranganath .S.Junpal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I want to have a Variable in a Session. In a Scenario i am fetching a variable from the session will replace back after incrementing it. But in the .SetAttribute we are allowed to have only the Objects. So i created an abject for int fromt the Wrapper Class ie Integer(0). Now i am able to set it in a Session.

Is it possible to convert this Integer(0) 's Object to its primitive type ie int. or is it possible to directly increment the object of the Wrapper Class with ++ Operater.


The code is as follows
-------------------------------------------------------------------------
HttpSession hSess= request.getSession(false);
hSess.setAttribute("counter",new Integer(0));
// new Integer Object is set in session

------------------------------------------------------------------------
Code to fetch and increment in a .jsp page (code in enclosed in a scriplets)
=----------------------------------------------------------------------
Integer YO= new Integer(0);

// fetching the Objct from the Session
YO=session.getAttribute("counter");
YO++// this fires error..

-----------------------------------------------------------------------

So please let me know if my code is fine else what be done to solve this problem.

Thank you
Regards
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a general question about java.lang.Integer -- you cannot increment it. Integer objects are immutable.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question really has little to do with sessions, and more to do with primitive wrapper calsess, so I've moved this to the Java in General (beginner) forum.
 
Puru Gujarati
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change Y++; to Y= new Integer(Y.intValue()+1);
 
Ranganath .S.Junpal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a Lot!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!