• 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
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

can we return null?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am reviewing the code and found that method returns null in the below code.
Please think as per code review.
Is it valid to return null as it may lead others to take care of Nullpointer Exception?

public Image getImage(Object item) {
try {
if (item instanceof IMarker) {
return getBreakpointImage((IBreakpoint) bp);
}

if(item instanceof IUDTThread){
return getUDTThreadImage((IUDTThread)item);
}
} catch (CoreException e) {
UDTDebugUIPlugin.logError(e);
}
return null;
}
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether it is valid to return null is up to your business logic. Sometimes you want to return null indicating something is wrong, sometimes you want to throw an exception.

For instance, java.util.Map's get method returns null if there is no key-value mapping for the specified key. java.util.List's get method on the other hand throws an IndexOutOfBoundsException if the index is invalid.


Should you choose to allow a null return value, you should document that this is a valid return value. Then programmers that call your method know that they should check if the return value is null:
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

Yes it can be considered a valid return technique, it just needs to be clearly documented in the Javadoc for that method e.g. "Returns x if one exists else null".
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As rightly pointed out by Rob, there are circumstances (rather language specifications) which need to be followed during implementation. Since, the pre-existing return statements return the appropriate values local to 'if' loops, 'return null' is added as per the java specifications - considering a situation if your control never enters the 'if' condition; since the return type of your method is Image (a subclass of the Almighty Object).
[ December 17, 2008: Message edited by: Nitin Pathak ]
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But as I also said, you can choose to throw an exception instead:

In the end, the choice is up to you.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreeing with Rob, I have another point in terms of code review. Methods should always have a single return statement i.e. create a reference, point it to whatever value you want as per your logic (including null) and finally return the reference at the end.
 
Sunil Kumar
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again that is my point of view. Others may differ
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's an old topic but still relevant. See this blog post: http://www.yegor256.com/2014/05/13/why-null-is-bad.html It claims that using (returning or accepting) NULL is a terrible practice
 
It's never done THAT before. Explain it to me tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic