E. Pryor
E. Pryor

Reputation: 23

SharedPreferences String name

I have been working on an app that scores teams based on how well they do in certain criteria, and I want it to save the teams data. So if the app closes, it doesn't lose their data. Here is my save Class

package com.example.elliott.alliancepicker;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.example.elliott.alliancepicker.MainActivity;
import com.example.elliott.alliancepicker.Question;
import com.example.elliott.alliancepicker.Team;

import java.util.ArrayList;


public class Save {
    String[] TeamBaseData = null;
    String[] TeamSplitData = new String[11];
    int[] TeamScoreData = new int[9];
    private static String prefsFileName = "prefs";
    private SharedPreferences prefs;
    String compileSave = "";
    public Save(Context context) {
    prefs = context.getSharedPreferences(prefsFileName,     Context.MODE_PRIVATE);
}
public void SaveTeamScore(){
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(prefsFileName, compileSave);
    editor.apply();
}
public Team regenTeam(){
    String toSplit = "";
    int questionScore = 0;
    TeamBaseData = (compileSave.split("@"));
    for (int i = 0; i < TeamBaseData.length; i++){
        toSplit = TeamBaseData[i];
        TeamSplitData = toSplit.split("%");
        for (int e = 2; e < TeamSplitData.length; e++) {
            int x = 0;
            toSplit = TeamSplitData[i];
            questionScore = Integer.parseInt(toSplit);
            TeamScoreData[x] = questionScore;
            x++;
        }
        if (TeamSplitData.length != 1) {
            Team newTeam = new Team(TeamSplitData[0], TeamSplitData[1], TeamScoreData);
            return newTeam;
        }
    }
    return null;
}
public SharedPreferences getPreferences() {
    return prefs;
}

public void setCompileSave(String compileSave) {
    this.compileSave = this.compileSave + compileSave;
}
}

Unfortunately... Every time I try to run my code it crashes and here is my exception

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.elliott.alliancepicker/com.example.elliott.alliancepicker.MainActivity}: 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.app.ActivityThread.performLaunchActivity(ActivityThread.java:2322)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5696)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
 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:169)
        at com.example.elliott.alliancepicker.Save.<init>(Save.java:24)

Thank you all for helping me solve my problem!

Upvotes: 1

Views: 230

Answers (2)

Derek Fung
Derek Fung

Reputation: 8211

Was your context passed to new Save() an Application context? Try changing it to Activity.

I.e. replace new Save(getApplicationContext()) with new Save(this) if your are in an Activity class, or replace with new Save(getActivity()) if you use it in Fragment class.

Upvotes: 1

challenger
challenger

Reputation: 2214

try to create your object on the main thread

Handler mainHandler = new         Handler(context.getMainLooper());

Runnable myRunnable = new      Runnable() {
@Override 
    public void run() {....} // This is your    code
};
mainHandler.post(myRunnable);

Upvotes: 0

Related Questions