Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Do you have elegant way of nullpointer exceptions

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in JSP or servlet?
 
Ranch Hand
Posts: 8943
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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/
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic