• 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

Servlet Listener class Not Working (Null Pointer Exception)

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote this code in Eclipse (Tomcat server), and i wrote this listener class. But it seems like it's not responding.
Here's my code:

LISTENER CODE:



SERVLET CODE:




ATTRIBUTE CLASS:




WEB.XML COdDE:





So, when i run this, the output is :

Test Context Attributes set by listener

that's it....        It doesn't invoke listener class i think??? I can't figure out where am i going wrong??? Help...
Also, in my web.xml file, i just wrote web-app, removing extra stuff that eclipse adds, because with the added stuff, it showed wrong.
that's not the problem however..

Any idea what's wrong....

 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What does "But it seems like it's not responding" mean? What does or does not happen? What did you expect to happen?
 
Tanish Goyal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the output, I'm expecting

Test Context Attributes set by listener
Dog's breed is: Great Dane



But i'm getting,

just Test Context Attributes set by listener



Now,My Servlet Listener class sets an attribute in the Servlet context called dog (of type Dog, whose code is supplied in the query). And, servlet class is programmed to access that attribute, and display it's value which is. Great Dane.

I don't see any flaw in my code, but i'm not getting the output I'm expecting. So, that's the issue, and i'm trying to figure out what's the issue .
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mentioned "Null Pointer Exception" - what is that about? If you're seeing one, post the full stack trace. Is it in line 18 of class ListenerTester?
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at MyServletContextListener, that shouldn't even compile - it implements neither contextInitialized nor contextDestroyed, both of which are required. Note that  "ContextInitialized" is capitalized incorrectly, and thus does not override anything. I strongly advise to use the "Override" annotation with all methods that are supposed to override or implement methods. That catches mistakes like this one early. But it does not explain why this class compiles at all, given that both required methods are missing.
 
Tanish Goyal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OMG... OMG...

YOU SAVED MY LIFE.

Can't believe I committed that minute, schmutz error.

Thank You so much...
 
Tanish Goyal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Mistake...

ContextInitialized

It should be, contextInitialized

Moreover, i didn't mention contextDestroyed().

 
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic