jeudi 22 mars 2018

Short form for Java If statement returns NullPointerException when one of the returned objects is null

Why this code returns the error: java.lang.NullPointerException

Object obj = null;
Long lNull = null;
Long res = obj == null ? lNull : 10L;

But the following way works without any errors:

Object obj = null;
Long res = obj == null ? null : 10L;

Aucun commentaire:

Enregistrer un commentaire