I'm trying to set an int value to request session like this...
request.getSession().setAttribute("score", 0);
request.getRequestDispatcher("game.jsp").forward(request, response);
But <% if(request.getAttribute("score") == null) %>
gives true, and I get NullPointerException
when I try to use this data. What could be a possible cause for this? Thank you.
The setAttribute()
method is looking for an object in the second parameter, but you are providing a primitive type. Try the following instead:
request.getSession().setAttribute("score", new Integer(0));
it should be..
request.getSession().getAttribute("score")
not
request.getAttribute("score")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.