Java short circuit evaluation

后端 未结 7 1509
深忆病人
深忆病人 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:35

    Advanced debugging lesson #1:

    If you run into a seemingly impossible error (e.g. one that contradicts you knowledge about Java), do the following:

    • Consult a reputable text book (or better still, the relevant standard) to confirm that your understanding is not flawed. (In this case your understanding was correct, and any half-decent textbook would confirm this in a minute.)

    • Check all of the stupid things that you could have done that could cause the impossible error. Things like not saving a file, not doing a complete build, running an old / stale version of the application, being in the wrong directory, and so on.

    In summary, learn to doubt yourself a bit more.

提交回复
热议问题