• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

session null pointer exception

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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