Question

I am checking how Radio button is working .. In my code I uses 2 radio buttons and when one is selected ... as I accept and another as not selected..when I am running the application is crashing and in logcat it is saying the error is null pointer exception and I am giving my code below .. please check

MainActiivyt

public class MainActivity extends Activity {

    RadioGroup group1;
    RadioButton button1;

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

        group1 = (RadioGroup) findViewById(R.id.rg1);

        int selected = group1.getCheckedRadioButtonId();
        button1 = (RadioButton) findViewById(selected);

        if ("I Accept".equals(button1.getText().toString())) {
            Toast.makeText(this,"Correct!",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this,"Incorrect.",Toast.LENGTH_SHORT).show();
        }

    }
}

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity" >    

    <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="I Accept" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="19dp"
        android:text="Not Accept"
        android:onClick="onRadioButtonClicked" />

    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="124dp"
        android:layout_toRightOf="@+id/rg1"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/rg1"
        android:layout_marginTop="172dp"
        android:text="TextView" />

</RelativeLayout>

logcat

02-28 11:57:20.835: E/AndroidRuntime(6713): FATAL EXCEPTION: main
02-28 11:57:20.835: E/AndroidRuntime(6713): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radiobutton/com.example.radiobutton.MainActivity}: java.lang.NullPointerException
02-28 11:57:20.835: E/AndroidRuntime(6713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at android.os.Looper.loop(Looper.java:137)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at java.lang.reflect.Method.invoke(Method.java:511)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at dalvik.system.NativeStart.main(Native Method)
02-28 11:57:20.835: E/AndroidRuntime(6713): Caused by: java.lang.NullPointerException
02-28 11:57:20.835: E/AndroidRuntime(6713):     at com.example.radiobutton.MainActivity.onCreate(MainActivity.java:25)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at android.app.Activity.performCreate(Activity.java:5104)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-28 11:57:20.835: E/AndroidRuntime(6713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-28 11:57:20.835: E/AndroidRuntime(6713):     ... 11 more
Was it helpful?

Solution

Try This code

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.radioButton1:
        if (checked)
            // RadioButton id 1
        break;
    case R.id.radioButton2:
        if (checked)
            //RadioButton id 2
        break;
}

}

OTHER TIPS

None of your RadioButtons are checked on onCreate so selected is -1 and as a result you find id for button1 using findViewById.

Add android:checked="true" to one of the RadioButton in the XML or change

button1 = (RadioButton) findViewById(selected);

to

button1 = (RadioButton) findViewById(R.id.radioButton1);

replace this radio group from your xml

<RadioGroup
        android:id="@+id/rg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:onClick="onRadioButtonClicked"
            android:text="I Accept" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="19dp"
            android:onClick="onRadioButtonClicked"
            android:text="Not Accept" />
    </RadioGroup>

This:

button1 = (RadioButton) findViewById(selected);

should be:

button1 = (RadioButton) findViewById(R.id.radioButton1);

And you miss a listener for when the RadioButton is checked... refer to the android developer page

Declare your radiobutton using its ID;

RadioButton button1 = (RadioButton)findViewById(R.id.radioButton1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top