• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

session null pointer exception

 
Donald Skidmore
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%!
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
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why do you have a "}" in your code?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic