0

I am trying to cast Object to Character in the ternary operator, but if object is null, I get NPE. But in general if I cast object, which is null, to Character it works. So my question is why we can't cast null object in ternary operator ?

Object[] objs = new Object[3];
        objs[0] = 'A';
        objs[1] = 'C';

        Character case1 = (Character) objs[2]; // ok 
        Character case2 = !Character.valueOf('A').equals(objs[0]) ? 'N' : (Character) objs[2]; // NPE

In example above I get NPE in case 2

Daulet Nurgali
  • 167
  • 1
  • 3
  • 8

0 Answers0