3

Not like C or C++, o = objectA.objectB.objectC will throw a NullPointerException if either object(A, B) is null in Java. In Java 1.7, I can't do anything like lambda expression to run this command with try-catch protection.

So, how will you perfectly cache the exception there?

eol
  • 23,236
  • 5
  • 46
  • 64
Tommy
  • 301
  • 2
  • 16
  • 3
    Possible duplicate of [Null check chain vs catching NullPointerException](http://stackoverflow.com/questions/37960674/null-check-chain-vs-catching-nullpointerexception) – shmosel Jan 18 '17 at 08:17
  • You'd better check if o == null before you use it. – Qian Sijianhao Jan 18 '17 at 08:19

3 Answers3

5

Sadly there is no sort of "propagate null" operator in Java, although it was talked about some while ago. (The notation o = objectA?.objectB?.objectC was mooted).

In your case you need to check each part in turn:

if (objectA == null){
    o = null;
} else {
   /*OType*/ p = objectA.objectB;
   o = p == null ? null : p.objectC;
}

Using purely the ternary conditional operator is also a possibility, but that means you need to write objectA.objectB in more than one place.

Enclosing the expression around a try catch block seems crude to me as it could smother legitimate NullPointerExceptions if the chain comprises functions (although that is a moot point for direct field access). But it is easy to read, and scales better for long chains:

try {
    o = objectA.objectB.objectC;
} catch (final java.lang.NullPointerException e){
    o = null;
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

how will you perfectly cache[catch?] the exception there?

I hope you mean avoid the null exception; not catching it.

You can write a utility that somewhat makes the check easier; i.e. a static utility method that takes variables arguments (array in diguise) and check if all are not null or not.

Example

public static boolean allNotNull(Object... objects) {
    for (Object o : objects) {
        if (o == null) {
            return false;
        }
    }

    return true;
}

if(allNotNull(objectA, objectB, objectC))
Ahmed Khalaf
  • 1,401
  • 2
  • 15
  • 29
  • 1
    How does this help for chained values? – shmosel Jan 18 '17 at 08:29
  • @shmosel No help for chained values, still need to check one by one. – Tommy Jan 18 '17 at 08:33
  • It doesn't. This is for a quick check for simple references like the one in the example. For chained values, I think any solution other than catching the exception will be re-inventing `Optional`. See the discussion @shmosel posted above. For Java 1.7, I think Guava's `Optional` can help. – Ahmed Khalaf Jan 18 '17 at 08:42
  • `Optional` would be an awfully bloated solution pre-lambda. – shmosel Jan 18 '17 at 08:44
0

Here's a one-line version after which o will either be null or the value of objectA.objectB.objectC:

Object o = (objectA != null && objectA.objectB != null) ? objectA.objectB.objectC : null;

I would also recommend against using try/catch here

rednoyz
  • 1,318
  • 10
  • 24