Android: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.contains(java.lang.String)' : <p>I am encountering an error here. I have designed an app, which involves a User Registration Page and Login Page. User Registration is happening successfully, with data also getting added and Posted to SQLITE.</p>

<p>However, during LOGIN, I am facing an issue. I have to open a new page, once the user logsin. But, the error which I am facing, due to sharedpreference is null pointer exception.</p>

<p>This is my login page , and after I hit the <code>login</code> button, I am encountering the error.</p>

<p><a href="https://i.stack.imgur.com/TG7dg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TG7dg.png" alt="enter image description here"></a></p>

<pre><code>com.example.dell.digitalwallet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.digitalwallet, PID: 3206
java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.contains(java.lang.String)' on a null object reference
    at com.example.dell.digitalwallet.Login$1.onClick(Login.java:56)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25881)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6649)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
</code></pre>

<p>06-02 12:39:50.866 3206-3206/com.example.dell.digitalwallet I/Process: Sending signal. PID: 3206 SIG: 9</p>

<p>I have created the following classes:</p>

<pre><code>Login.java
    package com.example.dell.digitalwallet;


import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class Login extends AppCompatActivity {
    private static TextView username;
    private static TextView password;
    private static EditText User_Name;
    private static EditText User_Password;
    private static Button login_btn, Registration, passwd_btn, Add_Person;
    private SharedPreferences sharedPreferences;


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Click_Button();
    }

    public  void Click_Button() {
        username = (TextView)findViewById(R.id.User_Name);
        password = (TextView)findViewById(R.id.Password);
        Registration = (Button) findViewById(R.id.button_registration);
        login_btn = (Button)findViewById(R.id.button_login);
        passwd_btn=(Button) findViewById(R.id.button_password);
        User_Name = (EditText) findViewById(R.id.Name);
        User_Password = (EditText) findViewById(R.id.User_Password);
        Add_Person = (Button) findViewById(R.id.add_person);


        login_btn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String username = User_Name.getText().toString();
                        String password = User_Password.getText().toString();

                        // Validate if username, password is filled
                        if(username.trim().length() &gt; 0 &amp;&amp; password.trim().length() &gt; 0){
                            String uName = null;
                            String uPassword =null;
                            if (sharedPreferences.contains(username))
                            {
                                uName = sharedPreferences.getString(username,"");

                            }

                            if (sharedPreferences.contains(password))
                            {
                                uPassword = sharedPreferences.getString(password, "");

                            }


                            if (username.equals(uName) &amp;&amp; password.equals(uPassword)){

                            Toast.makeText(Login.this,"User and Password is correct",
                                    Toast.LENGTH_SHORT).show();

                            //Starting the User's Home Page
                                Add_Person.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        startActivity(new Intent(Login.this, Parson_Details.class));
                                    }
                                });


                        } else {
                            Toast.makeText(Login.this,"User and Password is not correct",
                                    Toast.LENGTH_SHORT).show();

                            }
                          }


                    }
                }

        );

        Registration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Login.this, Users.class));
            }
        });

       passwd_btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               startActivity(new Intent(Login.this, Password.class));
           }
       }

       );

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

  }

Parson_Details.java

package com.example.dell.digitalwallet;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.annotation.Nullable;

import android.telephony.PhoneNumberFormattingTextWatcher;
import android.text.TextUtils;
import android.util.Log;
import android.util.Patterns;
import android.view.View;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

import java.lang.String;


public class Parson_Details extends AppCompatActivity {


    private String FirstName;
    private String MiddleName;
    private String LastName;
    private String Address;
    private int PhoneNumber;
    private int TotalCrBal;
    private int TotalDbBal;

    private static TextView First_Name;
    private static TextView Middle_Name;
    private static TextView Last_Name;
    private static TextView Address_Person;
    private static TextView Phone_Number;
    private static TextView Total_Cr_Bal;
    private static TextView Total_Db_Bal;
    private static Button Save;

    private static EditText First_N;
    private static EditText Middle_N;
    private static EditText Last_N;
    private static EditText Addr;
    private static EditText Phone_N;
    private static EditText Cr_Bal;
    private static EditText Db_Bal;
    private static Button save;

    public String getFirst_Name() {return FirstName;}
    public void setFirst_Name(String FirstName) {this.FirstName = FirstName;}

    public String getMiddle_Name() {return MiddleName;}
    public void setMiddle_Name(String Middle_Name) {this.MiddleName = Middle_Name;}

    public String getLast_Name() {return LastName;}
    public void setLast_Name(String Last_Name) {this.LastName = Last_Name;}

    public String getAddress() {return Address;}
    public void setAddress(String Address) {this.Address = Address;}

    public int getPhone_Number() {return PhoneNumber;}
    public void setPhone_Number(int Phone_Number) {this.PhoneNumber = Phone_Number;}

    public int getTotal_Cr_Bal() {return TotalCrBal;}
    public void setTotal_Cr_Bal(int TotalCrBal) {this.TotalCrBal = TotalCrBal;}

    public int getTotalDbBal() {return TotalDbBal;}
    public void setTotalDbBal(int TotalDbBal) {this.TotalDbBal = TotalDbBal;}


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homescreen);
        getSupportActionBar().hide();
        Click_Save_Button();

    }
        public  void Click_Save_Button() {

            First_Name = (TextView) findViewById(R.id.First_Name);
            Middle_Name = (TextView)findViewById(R.id.Middle_Name);
            Last_Name = (TextView) findViewById(R.id.Last_Name);
            Address_Person= (TextView) findViewById(R.id.Address);
            Phone_Number=(TextView) findViewById(R.id.Phone_Number);
            Total_Cr_Bal = (TextView) findViewById(R.id.Total_Cr_Bal);
            Total_Db_Bal = (TextView) findViewById(R.id.Total_Db_Bal);
            Save = (Button) findViewById(R.id.save);

            First_N = (EditText) findViewById(R.id.First_N);
            Middle_N = (EditText)findViewById(R.id.Middle_N);
            Last_N = (EditText) findViewById(R.id.Last_N);
            Addr = (EditText)findViewById(R.id.Addr);
            Phone_N=(EditText) findViewById(R.id.Phone_N);
            Cr_Bal = (EditText) findViewById(R.id.Cr_Bal);
            Db_Bal = (EditText) findViewById(R.id.Db_Bal);
            Save = (Button) findViewById(R.id.save);


    }
}
</code></pre>

<p>Parson_Details.java is not complete. I need to first reach to the homescreen of the added user, once the login button is clicked, and here, a new activity, called Add_Person has to be launched.</p>

<p><a href="https://i.stack.imgur.com/TG7dg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TG7dg.png" alt="Add Person Layout"></a></p>

<p><a href="https://i.stack.imgur.com/z9Kw5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z9Kw5.png" alt="enter image description here"></a></p>

<p>What exactly am I doing wrong?</p>
