• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null pointer exception in HFSJ for context listener

 
santosh malviya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello forum,

I am following Head First Servlet JSP and wrote according to the book for context listener but getting NullPointer exception in ListenerTester.java. Here is my code for same:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("test context attributes set by listener <br>");
out.println("<br>");
Dog dog = (Dog) getServletContext().getAttribute("dog"); // showing null here
out.println("Dog's breed is :" + dog.getBreed());
}

When I wrote this it was running well but after then it's showing null pointer exception. If some one have any idea then please help.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santosh malviya wrote:

Where are you setting your "dog" attribute? Is it not in the request?

Welcome to javaranch! Please UseCodeTags to for your source code. Also you can provide the Stacktrace but may not be necessary in this case.
 
santosh malviya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mohamed,

I am setting dog attribute in MyServletContextListener.java file which is as follows: -

public void contextInitialized(ServletContextEvent event) {
ServletContext sc = event.getServletContext();
String dogBreed = sc.getInitParameter("breed");
Dog d = new Dog(dogBreed);
sc.setAttribute("dog", d);
}

and following is my web.xml: -

<servlet>
<servlet-name>ListenerTester</servlet-name>
<servlet-class>com.example.ListenerTester</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListenerTester</servlet-name>
<url-pattern>/ListenTest.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>breed</param-name>
<param-value>Great Dane</param-value>
</context-param>
<listener>
<listner-class>
com.example.MyServletContextListener
</listner-class>
</listener>

In the book it's written if you get nullpointer then you have to see whether listener is being called or not, but I can't see this listener in log. Any pointer?

 
Prasad Krishnegowda
Ranch Hand
Posts: 667
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santosh malviya wrote:<listner-class>
com.example.MyServletContextListener
</listner-class>


Note that listner-class has to be listener-class.. Is it a typo mistake?
Can, you provide us the stack trace..
 
santosh malviya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Prasad, it worked, there was a typo in listener-class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic