Search code examples
androidandroid-activitysharedpreferences

Attempt to invoke interface method 'android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference


I am trying to save preferences so when user types in name and clicks save, it releases an alert dialog and if they say yes, it takes them to a welcome page. however, if they go back to the first page, they do not need to type name in again, it'll just say (WELCOME, USER). however its giving me an error, a null exception error.Please help! Also, I would need for the app to save the username, so when the user clicks the back button, and then goes to the first page to try and type in their names again, it just pushes them to the welcome page, since the name has been types before I will just add snippets of my code relevant to the question.

this is the page where the user types in name;

  import android.app.Activity;
    import android.app.AlertDialog.Builder;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.app.AlertDialog;
    import android.widget.EditText;

    public class userPreferences extends Activity {
    EditText editText;
    Button save;
    public static final String NAME="UserName";
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    String n;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userpreferences);
        editText=(EditText)findViewById(R.id.editText);
        save=(Button)findViewById(R.id.button);
        sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
      save.setOnClickListener(new View.OnClickListener() {
          final AlertDialog.Builder alertDialog = new AlertDialog.Builder(userPreferences.this);

          @Override
          public void onClick(View arg0) {

                      alertDialog.setTitle("Alert Dialog");
              alertDialog.setMessage("Are you sure you want to save?");
              alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              n=editText.getText().toString();
                              sharedPreferences=getPreferences(MODE_PRIVATE);
                              editor=sharedPreferences.edit();
                              editor.putString(NAME,n);
                              editor.apply();
                              startActivity(new Intent(userPreferences.this,Welcome.class));

                          }
                      });
                      alertDialog.setNegativeButton("No", null);
                      alertDialog.show();


          }
      });

    }


}

this is the welcome page;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Welcome extends Activity {

TextView textView;
SharedPreferences sharedPreferences;
public static final String NAME="UserName";
SharedPreferences.Editor editor;
String n;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);
    textView=(TextView)findViewById(R.id.textView);
    textView.setText("Welcome," + editor.putString(NAME, n));
    sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
    editor.apply();

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(Welcome.this, MainActivity.class));
    return;


}

the error in the logical reads

Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference

Please help!


Solution

  • Change from

    sharedPreferences=getPreferences(MODE_PRIVATE);
    

    to

    sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
    if(!sharedPreferences.getString(NAME,"Default value").equals("Default value")){
      startActivity(new Intent(userPreferences.this, Welcome.class));
      finish();
    }
    

    Also in your welcome class

    sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
    textView=(TextView)findViewById(R.id.textView);
    textView.setText("Welcome," + sharedPreferences.getString(NAME,"Default value"));
    editor = = sharedPreferences.edit();
    editor.putString(NAME, sharedPreferences.getString(NAME,"Default value"));
    editor.apply();