0

I added this Radio Group, with it's radioButtons, although now I'm getting that my whole Radio Group is null? A very interesting thing is, my app doesn't detect this error when i run it on a device with an API of 22, but when I RERUN it on the 28API emulator this error does appear. Here is the code:

radioGroup = findViewById(R.id.radioGroup2);
    Rd1= findViewById(R.id.radioButton5);
    Rd2= findViewById(R.id.radioButton6);
    radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (Rd1.isChecked()) {
                attemptLogin();
                Intent intent = new Intent(LoginActivity.this, BottomActivity.class);
                startActivity(intent);
            }else {
                if(Rd2.isChecked()) {
                    attemptLogin();
                    Intent intent = new Intent(LoginActivity.this, LoginActivity2.class);
                    startActivity(intent);
                }
            }

        }
    } );

Here is the XML code, I have tried adding the RadioGroup view, to my Login Activity, in order to place both buttons above each other, although once i did that i immediatly got this error.

<RadioGroup
            android:id="@+id/radioGroup2"
            android:layout_width="361dp"
            android:layout_height="125dp"
            android:layout_gravity="bottom"
            android:background="@color/licolor"
            android:gravity="center"
            android:layout_marginTop="20dp"
            android:orientation="vertical"
            tools:ignore="UselessParent">

            <RadioButton
                android:id="@+id/radioButton5"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginTop="16dp"
                android:background="@drawable/mybutton"
                android:gravity="center"
                android:text="@string/register"
                android:textColor="#fafafa"
                android:textStyle="bold"
                android:textSize="19sp"
                android:button="@null"/>

            <RadioButton
                android:id="@+id/radioButton6"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginTop="16dp"
                android:background="@drawable/mybutton"
                android:gravity="center"
                android:text="@string/login"
                android:textColor="#fafafa"
                android:textStyle="bold"
                android:textSize="19sp"
                android:button="@null"/>

        </RadioGroup>

And there you have the following error:

Attempt to invoke virtual method 'void android.widget.RadioGroup.setOnCheckedChangeListener(android.widget.RadioGroup$OnCheckedChangeListener)' on a null object reference
Tomas Mota
  • 672
  • 5
  • 27
  • It shows a null pointer exception. Check the line it is pointing to , to find out the null object. – Ravi Mar 06 '19 at 21:27
  • @Ravi i can't understand what it says, can you help? – Tomas Mota Mar 06 '19 at 21:28
  • Looks like it is unable to find your radio group. Check if you have imported it with the right id – Ravi Mar 06 '19 at 21:37
  • @Ravi i have man, on XML the radioGroup name is "radioGroup2", which corresponds to the Java one too – Tomas Mota Mar 06 '19 at 21:41
  • Please check your code. You have posted as "radioGroup = findViewById( R.id.radioGroup );" . It should be same in XML and Activity. – Ravi Mar 06 '19 at 21:42
  • @Ravi XML is the following The java- radioGroup = (RadioGroup) findViewById(R.id.radioGroup2); – Tomas Mota Mar 06 '19 at 21:46
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ricardo A. May 03 '19 at 19:48

2 Answers2

1

Your RadioGroup has an id = "radioGroup2"

android:id="@+id/radioGroup2"

And within the code, you are trying to find "radioGroup"

radioGroup = findViewById( R.id.radioGroup );

Just change it to:

radioGroup = findViewById( R.id.radioGroup2 );
0
//XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RadioGroup
    android:id="@+id/radioGroup"
    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:text="Radio Button 1"/>

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio Button 2"/>

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio Button 3"/>

</RadioGroup>

// Activity

RadioGroup radioGroup;
RadioButton RD1;
RadioButton RD2;
RadioButton RD3;

    radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
    RD1 = (RadioButton)findViewById(R.id.radioButton1);
    RD2 = (RadioButton)findViewById(R.id.radioButton2);
    RD3 = (RadioButton)findViewById(R.id.radioButton3);

           radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int checkedButtonId) {

            // This will get the radiobutton that has changed in its check state
            RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedButtonId);
            // This puts the value (true/false) into the variable
            boolean isChecked = checkedRadioButton.isChecked();
            // If the radiobutton that has changed in check state is now checked...
            if (isChecked)
            {
                switch (checkedButtonId)
                {
                    case R.id.radioButton1:
                        Log.i("test" , "Checked Button 1 ");
                        break;
                    case R.id.radioButton2:
                        Log.i("test" , "Checked Button 2 ");
                        break;
                    case R.id.radioButton3:
                        Log.i("test" , "Checked Button 3 ");
                        break;

                }
            }

        }
    });

And here you have the gradle file:

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.example.shrinkio"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
"android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-vector-drawable:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'android.arch.lifecycle:extensions:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation "com.google.android.gms:play-services-gcm:16.1.0"
implementation 'com.android.support:cardview-v7:28.0.0'

}

apply plugin: 'com.google.gms.google-services'
Tomas Mota
  • 672
  • 5
  • 27
Ravi
  • 881
  • 1
  • 9
  • 23