Proper way to handle Android Studio's NullPointerException lint warning

后端 未结 8 2033
终归单人心
终归单人心 2020-12-16 09:41

I\'m new to android/java programming and am confused how to properly deal with this warning.

Method invocation \'\' may produce \'Java.lang.NullPointe

8条回答
  •  孤城傲影
    2020-12-16 10:12

    Yes. Using if (Object != null){} for validating is the proper way. try {} catch (NullPointerException) {} is the next solution which is preferred in this case.

    If you want to get ride of it, throw an NullPointerException. Lint will ignore it in this case. public void myFunc() throws NullPointerException{}.

    Anyway, good Coding always means validating everything for a possible problem while runtime. Validating != null is just fine and should always be used whenever it's possible null.

提交回复
热议问题