aspose file tools*
The moose likes Servlets and the fly likes Null pointer exception in HFSJ for context listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Null pointer exception in HFSJ for context listener" Watch "Null pointer exception in HFSJ for context listener" New topic
Author

Null pointer exception in HFSJ for context listener

santosh malviya
Greenhorn

Joined: Apr 19, 2011
Posts: 3
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

Joined: Sep 08, 2007
Posts: 3097
    
  33

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.


Mohamed Sanaulla | My Blog
santosh malviya
Greenhorn

Joined: Apr 19, 2011
Posts: 3
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

Joined: Apr 25, 2010
Posts: 587
    
    3

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

Joined: Apr 19, 2011
Posts: 3
Thank you Prasad, it worked, there was a typo in listener-class.
 
 
subject: Null pointer exception in HFSJ for context listener