This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 3068
    
  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: 507

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


Regards, Prasad
SCJP 5 (93%)
santosh malviya
Greenhorn

Joined: Apr 19, 2011
Posts: 3
Thank you Prasad, it worked, there was a typo in listener-class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null pointer exception in HFSJ for context listener
 
Similar Threads
HTTP method GET is not supported by this URL
Error LlistenerStart
flush() before RequestDispatcher
Cannot get HttpSessionBindingListener method valueBound to trigger
attributes and listeners HF p171-176