Getting a Runtime Exception when set onSetCheckedListener on Switch

1.3k views Asked by At

I am getting this exception-
java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference

Here is my MainActivity code-

package com.example.toushif.myapplication;

import android.os.Bundle;
import android.app.Activity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;

public class MainActivity extends Activity implements 
CompoundButton.OnCheckedChangeListener {

CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
Switch sw1;
Switch sw2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cb1 = (CheckBox)findViewById(R.id.cb1);
    cb2 = (CheckBox)findViewById(R.id.cb2);
    cb3 = (CheckBox)findViewById(R.id.cb3);
    sw1 = (Switch)findViewById(R.id.switchh1);
    sw1 = (Switch)findViewById(R.id.switchh2);
    sw1.setOnCheckedChangeListener(this);
    sw2.setOnCheckedChangeListener(this);
    cb1.setOnCheckedChangeListener(this);
    cb2.setOnCheckedChangeListener(this);
    cb3.setOnCheckedChangeListener(this);

}


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    if(cb1.isChecked())
    {
        cb1.setText("Check Box 1 is checked");
    }
    else
    {
        cb1.setText("Check Box 1");
    }
    if(cb2.isChecked())
    {
        cb2.setText("Check Box 2 is checked");
    }
    else
    {
        cb2.setText("Check Box 2");
    }
    if(cb3.isChecked())
    {
        cb3.setText("Check Box 3 is checked");
    }
    else
    {
        cb3.setText("Check Box 3");
    }
    if(sw1.isChecked())
    {
        sw1.setText("Switch 1 ON");
    }
    else
    {
        sw1.setText("Switch 1 is OFF");
    }
    if(sw2.isChecked())
    {
        sw2.setText("Switch 2 ON");
    }
    else
    {
        sw2.setText("Switch 2 is OFF");
    }
}

}

Here is my layout-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout
    android:id="@+id/linear1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio Button 1"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio Button 2"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio Button 3"/>
    </RadioGroup>

</LinearLayout>

<LinearLayout
    android:id="@+id/linear2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_toRightOf="@id/linear1"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    >
    <CheckBox
        android:id="@+id/cb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check box 1"/>
    <CheckBox
        android:id="@+id/cb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check box 2"/>
    <CheckBox
        android:id="@+id/cb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check box 3"/>

</LinearLayout>

<LinearLayout
    android:id="@+id/linear3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/linear1"
    android:orientation="horizontal"
    >
    <Switch
        android:id="@+id/switchh1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch 1"/>
    <Switch
        android:id="@+id/switchh2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:text="Switch 2"/>

</LinearLayout>

<TextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This is simple text view."
    android:textSize="18dp"
    android:textStyle="bold"
    android:layout_below="@id/linear3"/>
<EditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/text1"
    android:textSize="18dp"
    android:textStyle="italic"/>
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/koala"
    android:layout_alignParentBottom="true"/>

I get the following exceptions when I run-

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.toushif.myapplication, PID: 4129
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.toushif.myapplication/com.example.toushif.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
                  at com.example.toushif.myapplication.MainActivity.onCreate(MainActivity.java:27)
                  at android.app.Activity.performCreate(Activity.java:6679)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6119) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Application terminated.

Please tell me where I went wrong. Thanks in advance :)

2

There are 2 answers

0
laalto On BEST ANSWER

You forgot to initialize sw2 but initialized sw1 twice:

sw1 = (Switch)findViewById(R.id.switchh1);
sw1 = (Switch)findViewById(R.id.switchh2);

The NPE comes when trying to invoke a method on sw2.

0
Bruno Ferreira On

you have an error in your oncreate() method.

You have this:

cb1 = (CheckBox)findViewById(R.id.cb1);
cb2 = (CheckBox)findViewById(R.id.cb2);
cb3 = (CheckBox)findViewById(R.id.cb3);
sw1 = (Switch)findViewById(R.id.switchh1);
sw1 = (Switch)findViewById(R.id.switchh2);
sw1.setOnCheckedChangeListener(this);
sw2.setOnCheckedChangeListener(this);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);

and your variables are:

CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
Switch sw1;
Switch sw2;

So sw2 is null bicause of that:

sw1 = (Switch)findViewById(R.id.switchh2);

Try change to :

sw2 = (Switch)findViewById(R.id.switchh2);