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

Check Parameters for Validity

 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been reading Effective Java book. It's a very good book, indeed. Is it actually necessary to check every parameter for a null and then throw a NullPointerException if the parameter is null? It seems that a method will be cluttered with a lot of if conditions if we need to do so.

Please share your opinion. Thanks
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends.

First of all, does it matter if the parameter is null? For instance, in java.io.File#listFiles(FileFilter), it uses the following to see if a file is accepted:

If you can do this, just let the parameter be null.

But let's assume it can't be null. If the method / constructor itself always calls a method on that parameter, I just let it throw the NPE for me. In all other cases I put in the check.

However, being the lazy guy that I am, I've created a utility method for that:

Called:

I have even a similar method for collections to check if any element is null as well.
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yup, I agree with that, and that's what I normally do. But according to the book, it's actually a good practice to check for a null instead of letting the parameter throws a NullPointerException as a result of referencing a null. That way, the debugging will be easier.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what Bloch means is that you need to avoid the case where you don't throw an NPE; if your method passes the null to something else which passes the null to something else which passes the null to something else which puts it in some record whence it is retrieved and throws an Exception . . .

You will have no end of difficulty working out what happened.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic