4 Replies Latest reply: Jul 10, 2012 5:16 PM by jschellSomeoneStoleMyAlias RSS

    Strange behaviour with shorthand null check

    947706
      Today I had a strange occurance of a NPE with that code:

      SomeType type = (person.getType() != null ? TYPE1 : _ TYPE_2);

      When person is not null but person.getType() returns null, I always faced a NullPointerException. Changing the code to

      SomeType type = (person.getType() == null ? TYPE2 : _ TYPE_1);

      solved the problem and worked. I'd like to know if anyone here can explain this strange behaviour.

      Regards

      PantheraOnca

      Edited by: user4174452 on 05.07.2012 22:22