• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointer Exception

 
Freeman Fridie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im attempting to do the excercise in chapter 5 of "Headfirst Servlets and JSP" regarding attributes and listeners. Im receiving the following error:

java.lang.NullPointerException
com.example.ListenerTester.doGet(ListenerTester.java:22)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I think I have everything as it a appears in the book. I have inlcuded all the necessary code below.

Here is my Servlet Code:

package com.example;


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ListenerTester extends HttpServlet
{
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>");

Dog dog = (Dog) getServletContext().getAttribute("dog");

Line 22out.println("Dog's breed is: " + dog.getBreed());
}
}

Here is my listener Code:

package com.example;

import javax.servlet.*;

public class MyServletContextListener
{


public void contextInitialized(ServletContextEvent event)
{
ServletContext sc = event.getServletContext();

String dogBreed = sc.getInitParameter("breed");

Dog d = new Dog(dogBreed);

sc.setAttribute("dog", d);
}

public void contextDestroyed(ServletContextEvent event)
{

}


}

Here is my dog class:

ackage com.example;

public class Dog
{
private String breed;

public Dog(String breed)
{
this.breed = breed;
}

public String getBreed()
{
return breed;
}
}

Finally my web xml file:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<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>

<listener-class>

com.example.MyServletContextListener

</listener-class>

</listener>



</web-app>

[ April 12, 2005: Message edited by: Freeman Fridie ]

[ April 12, 2005: Message edited by: Freeman Fridie ]
[ April 12, 2005: Message edited by: Freeman Fridie ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.NullPointerException
com.example.ListenerTester.doGet(ListenerTester.java:22)


Which is line 22?

Also, if you wrap your code in the UBB code tags, your indenting will be preserved. You will probably find that more people are willing to read it and help out when you do this.
There is a "CODE" button for generating the tag just below the text area where you type your messages.
 
Freeman Fridie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.


This is line 22

out.println("Dog's breed is: " + dog.getBreed());
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You listener doesn't implement ServletContextListener.
Try:

 
Freeman Fridie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much!!...that was it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!