Reputation: 1124
I want to edit my sharedpreferences in a method and save a value. If I do so, it says:
java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
I have an other activity where it worked (but the code was in the oncreate method). Here, the code doesn't seem to work. Here is my code:
public class LoginActivity extends ActionBarActivity {
SharedPreferences myPrefs;
//some irrelevant code here and there
public void login(View view){
//some more irrelevant code
bt_SignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String theusername = String.valueOf(username.getText());
String thepass = String.valueOf(pass.getText());
if (theusername.equals("schueler") && thepass.equals("123456")) {
SharedPreferences.Editor editor = myPrefs.edit();
editor.putBoolean("LehrerPref", false);
editor.apply();
Toast toast = Toast.makeText(getApplicationContext(), "Login erfolgreich! :D", Toast.LENGTH_SHORT);
toast.show();
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Login fehlgeschlagen :(", Toast.LENGTH_SHORT);
toast.show();
}
if (theusername.equals("lehrer") && thepass.equals("14869")) {
SharedPreferences.Editor editor = myPrefs.edit();
editor.putBoolean("LehrerPref", true);
editor.apply();
Toast toast = Toast.makeText(getApplicationContext(), "Login erfolgreich! :D", Toast.LENGTH_SHORT);
toast.show();
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Login fehlgeschlagen :(", Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
}
The parts within the if statements are the ones throwing the errors. Where is my mistake?
Upvotes: 1
Views: 130
Reputation: 10729
One of the most important things in programming is writing clear code. Don't duplicate code.
Here is sample solution:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_SignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String theusername = String.valueOf(username.getText());
String thepass = String.valueOf(pass.getText());
if (theusername.equals("schueler") && thepass.equals("123456")) {
setLehrerPref(false);
startFrontPage();
} else if (theusername.equals("lehrer") && thepass.equals("14869")) {
setLehrerPref(true);
startFrontPage();
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Login fehlgeschlagen :(", Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
private void setLehrerPref(boolean b){
SharedPreferences sharedPreferences = getSharedPreferences("LehrerPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("LehrerPref", b);
editor.apply();
}
private void startFrontPage(){
Toast toast = Toast.makeText(getApplicationContext(), "Login erfolgreich! :D", Toast.LENGTH_SHORT);
toast.show();
Intent i = new Intent(getApplicationContext(), Frontpage.class);
startActivity(i);
}
KISS :-)
Upvotes: 1