• 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

using null as arguments

 
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question is using null as argument to pass to the parameter a good (or okay) java convention?

I have a boolean method that takes 2 string parameters and inside the method I pass 2 string objects as argument and 1 non-existing String value (null)

Something like this.


Is it okay to use null as argument?
 
nick Mercado
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for typo, line 2 should be
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nick,

I don't see anything wrong with what you did in this situation.  In other situations you have to be careful to avoid a NullPointerException, but if the method is expecting null as a possible argument, then it's fine.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note also that before you use variables a or b, you should also check them to ensure they are not null, to avoid getting a NullPointerException.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the intention of the checks?

As it is now, the result of the method "checkParam" may depend on the particular order of the three parameters. Try, for instance, this:

Why do you get a NullPointerException in the third case?

And, as Fred says, check the parmeters a and b for a possible null value.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an alternative to having separate methods for the two and the three parameter cases, you could use a method with a veriable number of Strings as parameter.
For instnce:

But whether that is useful depends on what exactly you are trying to achieve.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your checkParam method will throw a NullPointerException when c is null and a is not equal to b.

Avoid writing this:

That's equivalent to what you wrote on lines 18-21.  You can simplify that to
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic