12

I want to ask why we get this annotation:

Method invocation getContext.getContentResolver() may produce NullPointerException

Why is it there and not in other parts of program Fragment/Activity? That approach has been used in tutorial made by Google - here is link for ContentProvider code https://github.com/udacity/Sunshine-Version-2/blob/sunshine_master/app/src/main/java/com/example/android/sunshine/app/data/WeatherProvider.java even if you create an aplication with just a blank activity and put that method in a newly created ContentProvider it is there.

Should we use getContext().getContentResolver().notifyChange(uri, null);outside ContentProvider getting the uri passed and then after the update/insert/delete is finished notifyChange? or maybe we can fix it somehow?

Radek O
  • 418
  • 1
  • 4
  • 12
  • It is just a simple warning if your IDE that `getContext()` could return `null` and `getContext().getContentResolver()` can then cause a `NullPointerException`. – Tom Oct 07 '15 at 10:46

6 Answers6

9

If you look in the source of ContentProvider (just hold SHIFT and click on the classname in Android Studio) then you will find that the implementation is holding an object of type Context as mContext.

Your solution is just the same, which means if mContext of ContentProvider is null, your reference will also be null. So there is no need for this.

To help you out, this is just a warning of your IDE if make such a construct yourself. But in this case there will always be context, because the ContentProvider is generated by your system. To avoid the error in your IDE just write @SuppressWarnings("ConstantConditions") above your class definition like:

...
@SuppressWarnings("ConstantConditions")
public class NoteProvider extends ContentProvider {
...
Mate
  • 418
  • 3
  • 13
  • 1
    Ignoring that kind of warnings in a whole class is really bad idea, at least you should use something like `//noinspection ConstantConditions` before that line of code - that turns off that warning only for next line of code – maresmar Mar 01 '18 at 15:28
8

If you can make sure that getContext() can never be null then you can simply ignore this warning. I think the warning even disappears of you just check for null:

if (getContext() != null) {
    getContext().getContentResolver();
}

You just have to keep in mind the code won't be executed if getContext() is null.

Cheers

edit: Be careful with the answer @Shivani Gupta gave you, because you could get different contexts. See: Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

Community
  • 1
  • 1
Manuel B.
  • 81
  • 5
3

Write getApplicationContext().getContentResolver() Hope this will work.

Shivani Gupta
  • 271
  • 3
  • 12
  • 1
    I can only use 'getContext().getApplicationContext().getContentResolver(); ' and the 'getContext().getApplicationContext()' party is producing the same result. – Radek O Oct 07 '15 at 23:01
1

Ok it seems I fixed it myself by declaring Context on the beggining of the class.

public class NoteProvider extends ContentProvider {
    Context context;

then initializing it in onCreate()

@Override
    public boolean onCreate() {
        mSQLiteOpenHelper = new NoteDbHelper(getContext());
        context = getContext();

        return true;
    }

I think that made sure that I always have Context when I use context.getContentResolver().notifyChange(uri, null); or retCursor.setNotificationUri(context.getContentResolver(), uri); in insert/update/delete/query method- retCursor being returned cursor by mentioned methods.

I have run the aplication on my phone and did not have issues yet if I will there will probably be an edit for this post.

EDIT:

It does not make a difference after all - explanationin answer by @Mate, thank you for that I think I get it now :]

Radek O
  • 418
  • 1
  • 4
  • 12
  • 1
    your solution is just tricking the IDE, getContext gives you a reference to mContext of ContentProvider, if this is null your context will also be null. But dont worry, it can't be null, see my answer. – Mate Oct 08 '15 at 20:16
1

According to ContentProvider getContext() docs:

Retrieves the Context this provider is running in. Only available once onCreate() has been called -- this will return null in the constructor.

So the getContext() method does not return null in insert(), update() or delete(), because onCreate() will be called before these calls.

So it's OK to disable that warning for that line if you use it in such case...

//noinspection ConstantConditions
getContext().getContentResolver().notifyChange(uri, null);
maresmar
  • 1,449
  • 14
  • 21
0

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
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175