I have such a problem. I have a class that works with Context , but this class itself does not inherit from Activity and therefore I just decided to transfer the Context to the function that needs it, and here the problem is before I put the Context into the function, I check it and it does not null , but when I check it inside the function that accepted it, then it’s already in it for some reason becomes null ... And I cannot create SharedPreferences without it. I don’t understand what kind of magic is this ... What am I doing wrong

That's how I pass it and it's not null

 UserAccess.saveAccess(getApplicationContext(), cursor); 

and here is the function that accepts it and in it is already null

 public static void saveAccess(Context context, Cursor cursor){ SharedPreferences sPref = context.getSharedPreferences("Compare",Context.MODE_PRIVATE); } 

And of course when I do getSharedPreferences() then there is also null How can this be?

Closed due to the fact that off-topic participants YuriySPb , pavlofff , insolor , zRrr , aleksandr barakin 27 Mar '16 at 14:25 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Yuriy SPb, pavlofff, insolor, zRrr, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 6
    pass context1, and use context from somewhere else) - Android Android
  • I apologize for the mistake, it was I who inadvertently put 1 ... now corrected the question. In the real code, this error does not exist and looks from since now after I fixed it ... - Aleksey Timoshchenko
  • getApplicationContext () sure you are not null? - Android Android
  • Try instead of getApplicationContext() to do MyActivity.this . Or where do you get it from? - Jarvis_J
  • Strange, drank tea, came back and it all worked)) Seriously ... I don’t even know how it happened ... - Aleksey Timoshchenko

1 answer 1

 public static void saveAccess(Context **context1**, Cursor cursor){ SharedPreferences sPref = **context**.getSharedPreferences("Compare",Context.MODE_PRIVATE); } 

Different variables. The signature declares the context1 variable, and trying to use context.

  • I apologize for the mistake, it was I who inadvertently put 1 ... now corrected the question. In the real code, this error does not exist and looks from since now after I fixed it ... - Aleksey Timoshchenko
  • How did you know that he is null? Was a NullPointerException thrown? If you getSharedPreferences() returns null, it means that the context is not just null. - Pavel Krizhanovskiy
  • Strange, drank tea, came back and it all worked)) Seriously ... I don’t even know how it happened ... - Aleksey Timoshchenko
  • But still, getSharedPreferences() still for some reason does not create a table ... Or creates, but I cannot understand where - Aleksey Timoshchenko