aspose file tools*
The moose likes Servlets and the fly likes Catching NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Catching NullPointerException" Watch "Catching NullPointerException" New topic
Author

Catching NullPointerException

Sanjay Anand
Ranch Hand

Joined: Apr 03, 2001
Posts: 42
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
L Goundalkar
Ranch Hand

Joined: Jul 05, 2001
Posts: 395
Please post the code snippet.


SCJP, SCWCD
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Sanjay Anand
Ranch Hand

Joined: Apr 03, 2001
Posts: 42
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

Joined: Oct 17, 2001
Posts: 4313

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

Joined: Apr 03, 2001
Posts: 42
Terribly sorry about that.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Sanjay Anand
Ranch Hand

Joined: Apr 03, 2001
Posts: 42
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

Joined: Jul 05, 2001
Posts: 395
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.
peter brews
Greenhorn

Joined: Feb 11, 2002
Posts: 20
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
Kev D'Arcy
Ranch Hand

Joined: Jul 26, 2001
Posts: 75
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 ]
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
Untrue; runtime exceptions are a subtype of java.lang.Exception. So NullPointerException will be caught by a catch(Exception e) {} statement.


Sun Certified Programmer for the Java 2 Platform
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You can prove it for yourself. Run the following code. The output will be "gotcha" which proves the nullPointerException was caught.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Catching NullPointerException