Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

NPE issue

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please advise reasons for NullPointerException in a servlet dealing with request.getParameter in Tomcat 6.0.20.
The below does work where the city value does forward and show in my JSP (pageOne.jsp or pageTwo.jsp).
But it also outputs the printStackTrace() NullPointerException on this one line everytime: if(city.equals("Boston")).



The NullPointerException can be eliminated if I do this:
String city = "Boston" so I assume something is wrong with the request.getParameter.

I was also able to show the city value without NullPointerException in the Servlet output page if I use the PrintWriter and comment out the conditions:


Please advise.
 
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If city is null, you'll get an NPE. If it isn't, you won't.

Catching exceptions in a servlet? Really?
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As i see the code, if you tell that city is not null after you get its value from the request, is not possible that your condition arises a NullPointerException.

Try to substitute the condition with the following one, just to eliminate any doubts:



if you keep getting the NPE the problem is not there
 
Bear Bibeault
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, if it's insignificant that city might be null, the old trick of:could be used.

But, it's not clear yet whether city being null is significant or not.
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic