Proper way to handle Android Studio's NullPointerException lint warning

后端 未结 8 1997
终归单人心
终归单人心 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:15

    What @Herrbert74 suggested it surely working fine, but sometimes it's better to not add a @SuppressWarnings("ConstantConditions") to an entire method (if it's not trivial), a better approach could be to use //noinspection ConstantConditions on the warned line.

    Those are my rules of thumb:

    • Use @SuppressWarnings("ConstantConditions") when the method is simple

    • Use //noinspection ConstantConditions when the method is complex and you need to remove the warning only on a specific line

提交回复
热议问题