• Post Reply Bookmark Topic Watch Topic
  • New Topic

using null as arguments  RSS feed

 
nick Mercado
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Sorry for typo, line 2 should be
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • 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.
 
Piet Souris
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • 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
Master Rancher
Posts: 2042
75
  • Mark post as helpful
  • send pies
  • 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.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!