Search code examples
javaandroidnullpointerexceptionsharedpreferences

How can i solve java.lang.NullPointerException problem?


I have two editText and one button in my MainActivity one of those edit text gets Username information and another one password. When ı clicked the button it conveys me to the another activity and in this activity there is a textview and a button this textview shows password and username information and when ı clicked the button it supposed to remove the username and password with editor.remove and take the user to the MainActivity but when ı clicked that button program is closing.

My MainActivity:

            package com.example.sploginekran;

            import androidx.appcompat.app.AppCompatActivity;
            import android.content.Intent; 
            import android.content.SharedPreferences;
            import android.os.Bundle;
            import android.view.View;
            import android.widget.Button;
            import android.widget.EditText;
            import android.widget.Toast;
            import javax.microedition.khronos.egl.EGLDisplay;

            public class MainActivity extends AppCompatActivity {
                Button buttonGiris;
                EditText editTextName,editTextPassword;
                SharedPreferences sp;
                SharedPreferences.Editor editor;
                @Override
                    protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);



                    buttonGiris = findViewById(R.id.buttonGiris);
                    editTextName = findViewById(R.id.editTextName);
                    editTextPassword = findViewById(R.id.editTextPassword);

                    sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
                    editor = sp.edit();


                    buttonGiris.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (editTextName.getText().toString().equals("admin") && 
                            editTextPassword.getText().toString().equals("123")) {
                            editor.putString("username",editTextName.getText().toString());
                           editor.putString("password",editTextPassword.getText().toString());
                           editor.commit();

                        Intent intent = new Intent(MainActivity.this, AnaEkranActivity.class);
                        startActivity(intent);
                      }
                else {
             Toast.makeText(getApplicationContext(),"Giriş Hatalı",Toast.LENGTH_SHORT).show();
             }
         }
    });
  }
}

My Second Activity:

            package com.example.sploginekran;

            import androidx.appcompat.app.AppCompatActivity;

            import android.content.Intent;
            import android.content.SharedPreferences;
            import android.os.Bundle;
            import android.view.View;
            import android.widget.Button;
            import android.widget.TextView;
            
            public class AnaEkranActivity extends AppCompatActivity{
            
            Button buttonCikisYap;
            TextView textViewCikti;
            SharedPreferences sp;
            SharedPreferences.Editor editor;
            String username,password;
                        
            @Override
            protected void onCreate(Bundle savedInstanceState) {


            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ana_ekran);

            buttonCikisYap = findViewById(R.id.buttonCikisYap);
            textViewCikti =  findViewById(R.id.textViewCikti);

            sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
            sp.edit();

            username = sp.getString("username","Kullanıcı Adı Yok");
            password = sp.getString("password","Şifre Yok");

            textViewCikti.setText(username+"-"+password);


            buttonCikisYap.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {

                    editor.remove("username");
                    editor.remove("password");
                    editor.commit();

                    startActivity(new Intent(AnaEkranActivity.this,MainActivity.class));
            }

         });
      }
    }

The error i get:

  java.lang.NullPointerException: 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

Also the error shows up in Second Activity on this line:

   editor.remove("username");
  

    



      

Solution

  • In your second activity AnaEkranActivity. You have this code:

            SharedPreferences.Editor editor;
    
    
            @Override
            protected void onCreate() {
                ...
                sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
                sp.edit();  // YOU MISSED ASSIGNING editor HERE
                ...
                buttonCikisYap.setOnClickListener(new View.OnClickListener() {
                    
                    @Override
                    public void onClick(View v) {
                      editor.remove("username"); // editor IS NULL HERE
                      ...
    

    You missed assigning an object to the editor variable. That's why you get a null pointer exception.

    The fix:

     sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
     editor = sp.edit();