web-dev-qa-db-fra.com

java.lang.NullPointerException: tentative d'appeler une méthode virtuelle sur une référence d'objet null

J'essaie de sauvegarder le nom du joueur dans les préférences partagées et de l'afficher dans une autre activité en le récupérant à nouveau dans les préférences partagées, mais mon application se bloque.

EXCEPTION FATALE: principale

 Process: plp.cs4b.thesis.drawitapp, PID: 1970
 Java.lang.RuntimeException: Unable to start activity ComponentInfo{plp.cs4b.thesis.drawitapp/plp.cs4b.thesis.drawitapp.PlayGame}: Java.lang.NullPointerException: Attempt to invoke virtual method 'Java.lang.String plp.cs4b.thesis.drawitapp.Player.getName()' on a null object reference
    at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2298)
    at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
    at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
    at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
    at Android.os.Handler.dispatchMessage(Handler.Java:102)
    at Android.os.Looper.loop(Looper.Java:135)
    at Android.app.ActivityThread.main(ActivityThread.Java:5221)
    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:899)
    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
 Caused by: Java.lang.NullPointerException: Attempt to invoke virtual method 'Java.lang.String plp.cs4b.thesis.drawitapp.Player.getName()' on a null object reference
    at plp.cs4b.thesis.drawitapp.PlayGame.onCreate(PlayGame.Java:20)
    at Android.app.Activity.performCreate(Activity.Java:5933)
    at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1105)
    at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2251)
    ... 10 more

Codes:

Player.Java

public class Player {

private Context context;
private SharedPreferences prefSettingsU;
private SharedPreferences.Editor prefEditorU;
private static final int PREFERENCE_MODE_PRIVATE = 0;
private static final String MY_UNIQUE_PREF_FILE = "DrawItApp";

public Player(Context context, String name) {
    this.context = context;
    saveName(name);
}

public void saveName(String n) {
    prefSettingsU = context.getSharedPreferences(MY_UNIQUE_PREF_FILE, PREFERENCE_MODE_PRIVATE);
    prefEditorU = prefSettingsU.edit();
    prefEditorU.putString("keyName", n);
    prefEditorU.commit();
}

public String getName(Context ctx) {
    prefSettingsU = ctx.getSharedPreferences(MY_UNIQUE_PREF_FILE, PREFERENCE_MODE_PRIVATE);
    String name = prefSettingsU.getString("keyName", "ANONYMOUS");
    return name;
}

PlayGame.Java

public class PlayGame extends Activity {

private TextView welcomePlayer;
private ListView createdGames;
private Player mPlayer;

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

    welcomePlayer = (TextView) findViewById (R.id.tvPlayerName);
    welcomePlayer.setText("Welcome Back, " + String.valueOf(mPlayer.getName(this)) + " !");

    createdGames = (ListView) findViewById (R.id.listCreatedGames);
    // adapter etc
    createdGames.setEmptyView(findViewById (R.id.tvNoGames));


}

PlayerName.Java

public class PlayerName extends Activity {

private EditText playerName;
private Player mPlayer;
public static Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player_name);
    context = this;
    playerName = (EditText) findViewById (R.id.etName);

}

public void onC_Confirm(View btnclick) {
    mPlayer = new Player(context, String.valueOf(playerName.getText()));
    //mPlayer.saveName();
    Intent intent = new Intent(PlayerName.this, PlayGame.class);
    startActivity(intent);
}

public void onC_testShPref(View btnclick) {
    Intent intent = new Intent(PlayerName.this, PlayGame.class);
    startActivity(intent);
}
34
ryckiazai

Votre application est bloquée à:

welcomePlayer.setText("Welcome Back, " + String.valueOf(mPlayer.getName(this)) + " !");

parce que mPlayer=null.

Vous avez oublié d'initialiser Player mPlayer dans votre activité PlayGame.

mPlayer = new Player(context,"");
50
M D