Attempt to invoke virtual method on a null object reference
<p>This is a script that counts how many times I pressed the button. When I run my code, I get this error:</p>

<pre><code>01-17 19:49:18.670 27354-27354/com.example.ben.testapp
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ben.testapp, PID: 27354
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.ben.testapp/com.example.ben.testapp.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.SharedPreferences
android.content.Context.getSharedPreferences(java.lang.String, int)'
on a null object reference
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2515)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'android.content.SharedPreferences
android.content.Context.getSharedPreferences(java.lang.String, int)'
on a null object reference
at
android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:184)
at
com.example.ben.testapp.MainActivity.numberOfClicks(MainActivity.java:89)
at com.example.ben.testapp.MainActivity.&lt;init&gt;(MainActivity.java:57)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1650)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 
at android.app.ActivityThread.access$900(ActivityThread.java:172) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5832) 
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:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
</code></pre>

<p>I have recently added code meant to save the amount of times I have clicked the button.</p>

<p>It looks like this:</p>

<pre><code>package com.example.ben.testapp;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

            }
        });
    }

    @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_main, 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);
    }


    private int clickNo = numberOfClicks();

    public void buttonClick(View view) {

        final TextView ClickCount = (TextView) findViewById(R.id.buttonClicks);

        clickNo = clickNo+1;

        if (clickNo == 10){
            Snackbar achievement10 = Snackbar.make(view,"acheivement unlocked: score of ten",Snackbar.LENGTH_LONG);
            achievement10.show();
        }

        if (clickNo == 20){
            Snackbar achievement20 = Snackbar.make(view,"acheivement unlocked: does your finger hurt?",Snackbar.LENGTH_LONG);
            achievement20.show();
        }

        if (clickNo == 50){
            Snackbar achievement50 = Snackbar.make(view,"acheivement unlocked: do you have a life?",Snackbar.LENGTH_LONG);
            achievement50.show();
        }

        String number = "" + clickNo ;

        ClickCount.setText(number);

        saveClicks();

    }

    private int numberOfClicks() {
        SharedPreferences prefs = getSharedPreferences("numberOf clicks",MODE_PRIVATE);
        return prefs.getInt("clicks", 0);
    }

    private void saveClicks(){
        SharedPreferences prefs = getSharedPreferences("numberOf clicks",MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("clicks", clickNo);
        editor.apply();
    }
}
</code></pre>