Java short circuit evaluation

后端 未结 7 1446
深忆病人
深忆病人 2021-01-07 23:26

I thought Java had short circuit evaluation, yet this line is still throwing a null pointer exception:

if( (perfectAgent != null) && (perfectAgent.ge         


        
7条回答
  •  执念已碎
    2021-01-07 23:49

    Java does have short circuit evaluation. Perhaps entry is null and so entry.getKey() is causing the NullPointerException. Another possibility is that getAddress() either returns null or has a NullPointerException happening inside somewhere (if it's more complicated than a simple return statement).

    EDIT: I see your edit where you claim this:

    More to the point, it is impossible to execute perfectAgent.getAddress() ...

    But what if perfectAgent.getAddress() is successfully executed and returns null? See what I mean...

提交回复
热议问题