This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
<%@ page session="true" %> <%! public String aUrl=null; public Vector domains= new Vector(); %> <% domains.add("1"); domains.add("2"); session.setAttribute("domains",domains); } System.out.println("Size of Domains=" + domains.size()); System.out.println("Session Domain Size=" + session.getAttribute("domains")); %> When I access this page the first time I have no problem. I open up another browser session a few minutes later and I get a null pointer exception on the last line. Does anybody know why this would happen? I am running Tomcat 5.0
I don't think you will get any NullPointeException from above line as you are saying.
Every time, when you make a request, _jspService(..) method of the generated servlet class calls pageContext.getSession() and it saves the session object to session implicit object. So session reference variable will always point to a HttpSession object.
There is no need of session. You can put your object in request scope and it also works in that way because domains is instance variable.
Check the same thing by request scope instead of session and tell us your output.