1

I'm receiving the following error message in LogCat:

   java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

I'm aware of what a NullPointerException is but not 100% on how to fix this with regards to passing the correct context. The error only happens when the app is runnning in the background (multitasking) A little guidance would be much appreciated. please Logcat and offending code below. THanks

Logcat:

Process: com.app.app, PID: 17519
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:537)
    at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:526)
    at com.app.app.DatabaseHandling.UpdateData.<init>(UpdateData.java:70)
    at com.app.app.PushService.PushReceiver$1.run(PushReceiver.java:94)
    at java.lang.Thread.run(Thread.java:764)

PushReceiver

UpdateData updateData = new UpdateData(MainActivity.mainActivity);

UpdateData:

 private final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.mainActivity);
astric mobiles
  • 468
  • 3
  • 14

3 Answers3

1

most likely (based upon the limited example provided, which does not even indicate, within which context that code runs), it should rather be:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

because one cannot just make up static fields, which do not exist -

while assigning Context classes (alike Activity or Context) to static fields is generally bad practice... and should be avoided, whenever just possible.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • I had previously attempted that, however, it displayed the error 'MainActivity is not an enclosing class' – astric mobiles Sep 13 '18 at 09:28
  • @astricmobiles if this code does not run within the `MainActivity`, that error message should be the result. `getApplicationContext()` may provide the `Context`, independent of an `Activity`. – Martin Zeitler Sep 13 '18 at 09:34
  • the variable definition is made with "private" so it is a global variable. It could also be a lifecycle problem, that might not be always solvable by using "MainActivity.this". – Thomas Richter Sep 13 '18 at 09:46
  • @ThomasRichter as the question is written, this could have various reasons (it completely lacks the structural context - just alike the code lacks the `Context`). – Martin Zeitler Sep 13 '18 at 09:51
  • exactly, it lacks for various reasons – Thomas Richter Sep 13 '18 at 10:23
1

I assume the problem is, that you try to initialise the SharedPreferences sp at the location, where you define it. You should define it first like:

private SharedPreferences sp;

After that, set this global variable sp in a function like "onReceive(Context context)":

sp = PreferenceManager.getDefaultSharedPreferences(context);

Or like already mentioned in the Activity itself in the function "onCreate(...)":

sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);

The problem might be, that you try to initialize a variable with a context (like activity), that is not available at this point, but in a later step of the lifecycle.

And avoid to hand over a context supplied by a static variable from another class.

Thomas Richter
  • 833
  • 8
  • 20
0

First you shuld check your code. you are passing MainActivity.mainAcitvity instead of MainACtivity.this or getApplicationContext()

private final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.mainActivity);

always pass context of activity in which your method works ...

Rohit Chauhan
  • 1,119
  • 1
  • 12
  • 30