How to trace a NullPointerException in a chain of getters

后端 未结 11 1543
谎友^
谎友^ 2020-12-03 06:46

If I get a NullPointerException in a call like this:

someObject.getSomething().getSomethingElse().
    getAnotherThing().getYetAnotherObject().getValue();
         


        
11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 07:28

    I generally do not chain getters like this where there is more than one nullable getter.

    If you're running inside your ide you can just set a breakpoint and use the "evaluate expression" functionality of your ide on each element successively.

    But you're going to be scratching your head the moment you get this error message from your production server logs. So best keep max one nullable item per line.

    Meanwhile we can dream of groovy's safe navigation operator

提交回复
热议问题