aspose file tools*
The moose likes JSP and the fly likes session null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "session null pointer exception" Watch "session null pointer exception" New topic
Author

session null pointer exception

Donald Skidmore
Greenhorn

Joined: Aug 27, 2002
Posts: 2
I have a jsp page that is baffling me.

<%@ 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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

"ddskid",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809

System.out.println("Session Domain Size=" + session.getAttribute("domains"));


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.

Naseem
[ July 25, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

<%!
public String aUrl=null;
public Vector domains= new Vector();
%>


This code is a recipe for disaster.

By putting the code in a declaration scriptlet you have created instance vairables that are shared across all the threads serving the JSP.

Is that what you intended?

Also, as this is not a Tomcat question, it's been moved to the JSP forum.
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536


why do you have a "}" in your code?


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session null pointer exception
 
Similar Threads
Unable to access ArrayList values from JSP
struts 2: how can i pass a Vector as a parameter?
Help, Vector not being passed correctly!
In Collection Vector problem
Error with jsp and JavaBeans