• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Exception handling for NullPointer and more  RSS feed

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, I have a question about exception handling and checking for status.
Lets say, a set of methods perform logic for a method.
Now some of the calls return instances, which can possibly be null.
Is it better to
a) check the return value as null
b) catch NULLPointerException
c) do not check or catch exception, as its a error you cannot recover from.
My doubt is how much error/exception checking should be done. On one hand, the more the checking for errors(Null pointers, failed operations), the better the code is. On the other hand, its advised to not catch Exceptions, if they cannot be handled (specially runtime).
Please advice,
- Avianu
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's best practice to check for null before operating an object, if that Object can be null sometimes.
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a subtle question. The answer is that the best thing to do depends on the situation. In general, catching NullPointerException at a low level is a bad thing to do, because it will hide programming errors -- i.e.,

Here the catch block hides an obvious programming error. On the other hand, catching them at the top level of an application may make perfect sense:

Now, the question is, should you check things for null, or catch NullPointerException? This depends on whether the variable might be null in the normal course of events (calling getParent() on a Swing component might return null) or whether a null value would indicate a programming error (the variable s in the first example shouldn't be null.) In the first case, you should check for null. In the second case, you should not. In neither case should you catch NullPointerException at a low level. It may always make sense to catch it at a high level, however (or catch all RuntimeExceptions together.)
 
Avianu Sud
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,
Very good points.
I would like to comment on a few.
I agree that catching NullPointerException at lower level can hide programming errors, or introduce more errors.
On the other hand take for example a method(low level) that makes multiple calls each of which can return null value. Now, checking for null after every call can clutter the code(make it less readable). Exceptions are pitched as one way to avoid code clutter!
For example Ideally I would write this:
DAOObject daorecord = daofactory.getDAOObject("emprecord");
if(daorecord != null) {
String firstdata = daorecord.getFirstData();
if( firstdata != null ) {
System.out.println( fistdata.somemethod() );
.....
} else {
return false;
}
}
The other Alternative is:
try {
DAOObject daorecord = daofactory.getDAOObject("emprecord");
String firstdata = daorecord.getFirstData();
System.out.println( fistdata.somemethod() );
} catch (NullPointerException e) {
return false;
}
The last alternative is not to put any try/catch, and let the null pointer propogate. But that can be potentially dangerous, as some method might be able to catch the fix the condition!.
It gets tricky! Any tips are appreciated.

- Avianu
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now this is yet another situation. Here, apparently the nulls occur in the normal course of running the application -- i.e., these methods are documented to return null. In such a case, you should always check for null and take some recovery action. Catching NullPointerException is not a substitute for this. Exceptions should always indicate an error, and never be used simply as a shortcut.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!