-2

I have been banging my head on the keyboard for hours. I want to store an incremented integer for a separate stats activity in my app. I am trying to use Shared Preferences to achieve this. However in my latest attempt, which is the furthest I have gotten, the program throws an exception.

My code:

public class LootChest extends AppCompatActivity {
    public static final String prefName = "prefsFile";
    SharedPreferences settings = getApplicationContext().getSharedPreferences(prefName, 0); //line 25
    int rollCountS = settings.getInt("Roll Count", 0);
    int rollCount = 0; //to be incremented

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loot_chest);
    }

    public void openChest(View v) {
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("Roll Count", rollCount);
        rollCount = rollCountS + 1;

The exception:

Caused by: java.lang.NullPointerException:
Attempt to invoke virtual method
'android.content.SharedPreferences android.content.Context
.getSharedPreferences(java.lang.String, int)'
on a null object reference at android.content.ContextWrapper
.getSharedPreferences(ContextWrapper.java:171)
at net.zingrook.mobiloot.LootChest.<init>(LootChest.java:25)

I've read dozens of threads on implementing this, and looked at the Android documentation and I am out of ideas. Thank you for any help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
zingrook
  • 25
  • 5
  • I'm not an android programmer, but if the issue is the Context wrapper is null in class instantiation, is it possible the settings should be after the `onCreate` is called? – KevinO May 25 '17 at 04:30

3 Answers3

4

You are initializing sharedPreference object before activity lifecycle could begin.

move the initialization into onCreate

public static final String prefName = "prefsFile";
    SharedPreferences settings;  //line 25
    int rollCountS;
    int rollCount = 0;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loot_chest);
        settings = getApplicationContext().getSharedPreferences(prefName, 0); 
    }
Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
1

Just change this your code in this way.

public class LootChest extends AppCompatActivity {
    public static final String prefName = "prefsFile";
    SharedPreferences settings;  //line 25
    int rollCountS;
    int rollCount = 0; //to be incremented

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loot_chest);
        settings = getApplicationContext().getSharedPreferences(prefName, 0);
        rollCountS = settings.getInt("Roll Count", 0);
    }
Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33
0

My Point of view

public class LootChest extends AppCompatActivity {
public static final String prefName = "prefsFile";
SharedPreferences settings;
SharedPreferences.Editor editor;
int rollCountS;
int rollCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loot_chest);
    settings=getApplicationContext().getSharedPreferences(prefName, 0);
    editor = settings.edit();
    rollCountS= settings.getInt("Roll Count", 0);
}

public void openChest(View v) {
    editor.putInt("Roll Count", rollCount);
    rollCount = rollCountS + 1;
Saravanakumar
  • 391
  • 1
  • 3
  • 5