Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Catching NullPointerException

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a curious problem. More academic at this point than anything else.
I am writing a login servlet.
We are required by our client to not allow the user / customer to see any exceptions at all. Users need simply be redirected to a 'temporary difficulties' html page.
So, in addition to other checks, we've put in a method-wide try - catch in the doPost method that logs the error and redirects the user in case something untoward occurs.
Now, in updating and tweaking the code, I made an error in logic that was caught only in runtime. I was trying to call methods for an object before instantiation. No problem. It is now fixed.
But the question is, why was the runtime NullPointerException not caught? Why did it spit up to the browser level immediately? I even tried catching the NPE explicitly, to no effect.
Any thoughts on this?
I can post code snippets if anyone thinks them relevant.
Sanjay
 
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the code snippet.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a workaround -- you could define an <error-page> in your web.xml so that every 500 Internal Servlet Error or every NullPointerException; it automatically sends your client to a "Temporary troubles" page.

From the Servlet 2.3 spec:
<!--The error-page element contains a mapping between an error code or exception type to the path of a resource in the web application -->
<!ELEMENT error-page ((error-code | exception-type), location) >
<!--The error-code contains an HTTP error code, ex: 404 -->
<!ELEMENT error-code (#PCDATA) >
<!--The exception-type contains a fully qualified class name of a Java exception type. -->
<!ELEMENT exception-type (#PCDATA) >
<!--The location element contains the location of the resource in the web application. -->
<!ELEMENT location (#PCDATA)>


[ May 31, 2002: Message edited by: Jessica Sant ]
 
Sanjay Anand
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jessica for a good suggestion.
It does not solve my academic question and it is also not usable because my servlet container, Macromedia's JRun, does not support the specs required for the error page functionality.
And, "L" (a name not in keeping with the JavaRanch custom by the way). Here's a code snippet. I hope it formats correctly and is readable.

[ May 31, 2002: Message edited by: Sanjay Anand ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sanjay Anand:
And, "L" (a name not in keeping with the JavaRanch custom by the way).


Actually we do allow FirstIntial <space> LastName but NOT FirstName <space> LastInitial
Thanks for looking out for us though!
 
Sanjay Anand
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terribly sorry about that.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no reason why this shouldn't work. Exception should catch everything. Perhaps there is a bug in the servlet container? Have you checked with Macromedia?
 
Sanjay Anand
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I have not checked with Macromedia. I will do that. Thanks for the suggestion.
I am heartened to know that there isn't a bug in my code at any rate.
Sanjay
 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have worked on JRun 4. Sometimes it gives exceptions like this and also ClassCastException for no bug in the code.
You need to shut down the servers and even the admin one and restart then through Windows Services. Then it will work fine.
I think its a bug in JRun container.
Same querries are unanswered in JRun user forum.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jessica,
I would also like to know if there is any similar solution possible for EJB's. I have methods defined in EJB and after caching the exception i just need to show the users a generic error message instead of the actual one.
Thanks,
pete
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might be wrong here, but you could add this after your ]. An exception catch won't catch Null Pointers, you need to catch throwables to get nulls.

[ June 04, 2002: Message edited by: Thomas Paul ]
 
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Untrue; runtime exceptions are a subtype of java.lang.Exception. So NullPointerException will be caught by a catch(Exception e) {} statement.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can prove it for yourself. Run the following code. The output will be "gotcha" which proves the nullPointerException was caught.
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic