-6

What is the best way to avoid multiple if blocks which is used for null checks in Java?

The following is my sample code. Which one is the most optimized way?

if (address!=null} {
    if (firstName!=null) {
        if (lastName!=null) {

        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chinnu
  • 59
  • 2
  • 9
  • Different approach, use the required option in your HTML form. Take a look here [link]http://webdesign.about.com/od/html5tags/p/required-attribute.htm[/link] (there are many other sources but this one just showed up first) – RST May 07 '13 at 06:04
  • 2
    Careful with client-side checking, it is never sufficient, if he is not running an html5 browser, the required will be useless – MisterJ May 07 '13 at 06:05
  • Your sample code doesn't even compile. – Peter Mortensen Sep 19 '15 at 18:57

6 Answers6

10

Use &&. && is logical and. && combines two values and returns a boolean which is true if and only if both of its operands are true

if(address!=null && firstName!=null && lastName!=null)
{
}

For instance

boolean b;
b = 3 > 2 && 5 < 7; // b is true
b = 2 > 3 && 5 < 7; // b is now false
Linga
  • 10,379
  • 10
  • 52
  • 104
7

Use and operator (&&)

   if(address!=null && firstName!=null && lastName!=null) 
    {
      //DoSomething here 
    }

And I suggest you to see Short circuit evaluation

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
7

if loop is a wrong word. You should say if statements
As in you case you can use OR (||) or AND (&&)statement like this

if(address!=null && firstName!=null && lastName!=null)
{
}


Try AND(&&) if you want to pass all checks or intead of nested if statements and try OR(||) for non nested like else if or simply say if you want to pass anyone of your condition

But if all of these are Strings then you should try like this
"yourValue".equals(stringValue)
This will skip the null check.

Freak
  • 6,786
  • 5
  • 36
  • 54
  • @downvoter please care to comment.otherwise i will flag it to the moderator – Freak May 07 '13 at 06:08
  • 1
    don't flag it for moderation, because it would be declined. But yes downvoter should leave comment IMO – Habib May 07 '13 at 06:12
  • ok i dint know about this.I thought here we can flag for any type of attention to the moderator.thanks bro :) – Freak May 07 '13 at 06:14
3

there are no if LOOPS

boolean complete = address != null && firstName != null && lastName != null;
if (complete) 
{
}
Marc
  • 6,051
  • 5
  • 26
  • 56
1

What about:

public boolean notNulls(Object ... args) {
    for(Object arg : args)
        if (arg == null) return false;
    return true;
}

Use:

if (notNulls(address, firstName, lastName)) {
    // do something
}
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
-1

As others point out, a logical and (&&) is probably the best way to consolidate your logic. An && operation will only evaluate to true if both sides evaluate to true.

if (address != null && firstName != null && lastName != null) {
    // Whatever you want to do with that...
} else {
    // Whatever you want to do with bad input
}

For the sake of diversity, you could also use a try-catch approach. In Java, a NullPointerException will be thrown if you try to call a method on a null value, which you can catch and handle.

try {
    // Whatever you want to do with that...
} catch (NullPointerException npe) {
    // Whatever you want to do with bad input
}

This approach can be helpful if you've got a really big set of inputs that might be null, although in general I wouldn't advocate it. (The problem with the second approach is that if you call some other method from the try part that triggers a NullPointerException, it will end up in the catch block here, even though it may be totally unrelated to these inputs - i.e. you could make it hard for yourself to spot a bug in a different part of your program.)

killscreen
  • 1,657
  • 12
  • 14
  • Code should never throw `NullPointerException` under normal circumstances. A catch block may hide the original error, causing other more subtle errors in its wake. [Strict Exception Rules](http://pmd.sourceforge.net/pmd-4.2.6/rules/strictexception.html) – Paul Vargas May 07 '13 at 06:15
  • I concur (that's what I was getting at in my last paragraph, albeit not so succinctly). It is necessary to understand what _can_ be done in a language before understanding what _should_ be done; since the OP self-identifies as a beginner at Java, I think this is relevant. – killscreen May 07 '13 at 06:28