• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Do you have elegant way of nullpointer exceptions

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in JSP or servlet?
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use an Error page to display the error if it occurs in JSP.
In servlets you can make an entry in the web.xml and map the exception to some error page.
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i find with nullpointers it's normally best to check variables against null before you use them
if(a != null)
a.something();
else
out.print("some error");
there are so many different nulls that can occur in one program i don't think a single page will be that helpful ?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Tim that the most elegant way of daling with NPEs is to not have them in the first place. Anytime an NPE occurs, it's an error in your program.
 
Jean Miles
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks that helps. we should avoid runtime exceptions and if they do happen how they should be handled.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's an ancient practice to return null to indicate some kind of failure or problem, probably dating back to the first days of C. This forces you to check for nulls. But there is a "null object" pattern that you can use. Instead of returning null, return a special instance or subclass of the expected object with the special behavior. Frinstance:

You could change getPage to return a NullPageObject which extends PageObject to always return "" and eliminate the test for null.
Of course this mostly applies to where you are in control of introducing the null pointers in the first place. And it might be a strategy for handling some that are created by code that you cannot change.
Here's an academic discussion ... I just Googled for "null object pattern" http://www.cs.oberlin.edu/~jwalker/nullObjPattern/
 
I didn't say it. I'm just telling you what this tiny ad said.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic