Barun Halder
Barun Halder

Reputation: 85

Implementing View.OnclickListener, referring setOnClickListener to 'this' variable......causes NullPointerException and failed to start Activity

the error log is like below

05-01 12:33:41.993    3145-3145/com.example.barun.myapplication D/AndroidRuntime﹕ Shutting down VM
05-01 12:33:41.993    3145-3145/com.example.barun.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.barun.myapplication, PID: 3145
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.barun.myapplication/com.example.barun.myapplication.TextPlay}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.barun.myapplication.TextPlay.onCreate(TextPlay.java:32)
        at android.app.Activity.performCreate(Activity.java:5937)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

I am a beginner in Android, I tried a little different way(by implementing View.OnClickListener and referring the class itself) to call setOnClickListener() like below in TextPlay class, it is failing.

Method 1:

public class TextPlay extends Activity implements View.OnClickListener {
    Button chkCmd;
    ToggleButton passTog;
    EditText input;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);

        baconAndEggs();
        chkCmd.setOnClickListener(this);
        passTog.setOnClickListener(this);

    }

    private void baconAndEggs() {
         Button chkCmd = (Button) findViewById(R.id.bResult);
         ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
         EditText input = (EditText) findViewById(R.id.etCommands);
         TextView display = (TextView) findViewById(R.id.tvResults);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bResult:
                break;
            case R.id.tbPassword:

                break;
        }
    }
}

But if I don't implement View.OnClickListener and do create a new instance of View.OnClickListener as a parameter of setOnClickListener() method, it does work fine.

Method 2:

    chkCmd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //required code here
        }
    });

Why is it so? I have followed this Youtube link and also some blogs where both Method 1 & 2 is fine as per their advise but for me only Method 2 is working, on Method 1 it is runtime error.

Upvotes: 0

Views: 1253

Answers (1)

Mattia Maestrini
Mattia Maestrini

Reputation: 32790

Inside baconAndEggs you create a local variable for every object, you are not assigning the class variables. Because of that the class variables remains null.

Change from this:

private void baconAndEggs() {
     Button chkCmd = (Button) findViewById(R.id.bResult);
     ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
     EditText input = (EditText) findViewById(R.id.etCommands);
     TextView display = (TextView) findViewById(R.id.tvResults);
}

to this:

private void baconAndEggs() {
    chkCmd = (Button) findViewById(R.id.bResult);
    passTog = (ToggleButton) findViewById(R.id.tbPassword);
    input = (EditText) findViewById(R.id.etCommands);
    display = (TextView) findViewById(R.id.tvResults);
}

Upvotes: 1

Related Questions