Checking for zeros using if statements and exception handling

String s = foo.getBar().getFrobz().getNobz().getBaz();

I want to return the value getBaz(). The problem is that any of the other methods can return null- in this case, I just want to return an empty string.

Here is one way to achieve this effect:

String s = "";

if (foo.getBar() != null ) {
    if (foo.getBar().getFrobz() != null) {
        if (foo.getBar().getFrobz().getNobz() != null) {
            if (foo.getBar().getFrobz().getNobz().getBaz() != null) {
                s = foo.getBar().getFrobz().getNobz().getBaz();
            }
        }
    }
}

And here is another simple way:

String s = "";

try {
    s = foo.getBar().getFrobz().getNobz().getBaz();
} catch (NullPointerException npe) {

}

I heard that exceptions are more expensive than statements, but they looked cleaner. Maybe this issue can be solved differently? Any help is appreciated.

+4
source share
6 answers

The correct answer here is to simply use nested if conditions. There are no other good solutions, and it is not recommended to use exceptions because I am not trying to handle exceptions here - I am just trying to assign a value.

0

Optional? . :

return Optional.ofNullable(foo.getBar());

if(optional.isPresent()) {
    // do something with the value
}

, Java 8. , null . Exception, null .

, foo.getBar().getFrobz().getNobz().getBaz() " " . .

"":

public class Main {

    public static void main(String[] args) {
        Foo foo = new Foo();

        final Optional<String> s = foo.getBar()
                .flatMap(Bar::getFrobz)
                .flatMap(Frobz::getNobz)
                .flatMap(Nobz::getBaz);
        if(s.isPresent()) {
            System.out.println("present");
        } else {
            System.out.println("not present");
        }
    }

    private static class Foo {
        Bar bar;

        public Optional<Bar> getBar() {
            return ofNullable(bar);
        }
    }

    private static class Bar {
        Frobz frobz;

        public Optional<Frobz> getFrobz() {
            return ofNullable(frobz);
        }
    }

    private static class Frobz {
        Nobz nobz;

        public Optional<Nobz> getNobz() {
            return ofNullable(nobz);
        }
    }

    private static class Nobz {
        String baz;

        public Optional<String> getBaz() {
            return ofNullable(baz);
        }
    }
}

, .

+1

, , .

, :

foo.getBar();

... bar, , null:

public String getBar() {
    String result = getFrobz();
    return result == null ? "" : result;
}

, , , , , null.

0

, :

Bar bar;
Frobz frobz;
Nobz nobz;
String baz;
if (null != (bar = foo.getBar()) &&
    null != (frobz = bar.getFrobz()) &&
    null != (nobz = frobz.getNobz()) &&
    null != (baz = nobz.getBaz())) {
    s = baz;
}
0

, :

String s = Optional.ofNullable(foo.getBar())
        .map(Bar::getFrobz)
        .map(Frobz::getNobz)
        .map(Nobz::getBaz)
        .orElse("");
0

, . NPE, NPE, , , ifs .

Because they do different things, they serve different uses. Catch the exception only if you want the empty string to not matter where the NPE comes from another, use nested ifs. Others have suggested ways to reduce nesting depth.

-2
source

All Articles