Android: getContext().getContentResolver() sometimes gets NullPointerException

后端 未结 7 1727
北恋
北恋 2021-02-20 02:05

I want to ask why we get this annotation:

Method invocation getContext.getContentResolver() may produce NullPointerException

Why i

7条回答
  •  囚心锁ツ
    2021-02-20 02:51

    Whenever you try to use a member or a method of an object, you can have a runtime exception if the object, whose member/method you try to use is null. Let's suppose you want to use a member/method of an object, obj. If you use it like this:

    if (obj != null) {
        //use members/methods of obj
    }
    

    then you prevented the problem. However, you might want to handle it as an exception, like this:

    try {
        //use members/methods of obj
    } catch (NullPointerException npe) {
        //handle the NullPointerException
    }
    

提交回复
热议问题