Jessica Adams
Jessica Adams

Reputation: 27

Android Studio: Passing RadioButtons using Intent ERROR NullPointerException

I am a beginner so i apologize in advance I get the following Error when running this program:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.RadioGroup.getCheckedRadioButtonId()' on a null object reference

here is my code for "Quiz"

public class Quiz extends Activity {
Button btn;
RadioGroup rg;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;

then below that: 2

And the xml for "quiz":

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/rg">
<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 1"
    android:id="@+id/rb1"
    android:layout_marginBottom="55dp"
    android:layout_above="@+id/rb2"
    android:layout_centerHorizontal="true"
    android:checked="false" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 2"
    android:id="@+id/rb2"
    android:layout_above="@+id/rb3"
    android:layout_alignLeft="@+id/rb1"
    android:layout_alignStart="@+id/rb1"
    android:layout_marginBottom="58dp"
    android:checked="false" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 3"
    android:id="@+id/rb3"
    android:layout_above="@+id/rb4"
    android:layout_alignLeft="@+id/rb2"
    android:layout_alignStart="@+id/rb2"
    android:layout_marginBottom="57dp"
    android:checked="false" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 4"
    android:id="@+id/rb4"
    android:layout_marginBottom="71dp"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/rb3"
    android:layout_alignStart="@+id/rb3"
    android:checked="false" />
</RadioGroup>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Question:"
    android:id="@+id/textView3"
    android:layout_marginTop="39dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/nextBtn"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

Second activity (Quiz1): 3

any help is appreciated thank you lots <3

Upvotes: 0

Views: 537

Answers (2)

Fergus Wang
Fergus Wang

Reputation: 46

RadioGroup rg need a init step to check a RadioButton before invoke getCheckedRadioButtonId().

Like this:

RadioGroup rg;
RadioButton rb1;
rg = ....
rb1 = ....
// check a RadioButton first
rg.check(R.id.rb1);

And make sure that your id in "findViewByid" method is similar to RadioGroup's id in Layout Xml.

Upvotes: 1

Ramazan Cinardere
Ramazan Cinardere

Reputation: 133

The problem is, you don't init your RadioGroup button radioGroup in your Quiz1 class.

Upvotes: 0

Related Questions