noobishProgrammer
noobishProgrammer

Reputation: 117

Null pointer exception problem with grabbing text from radio button from xml

I am trying to grab male female text from this radio button, but it give me a null pointer exception, and I am just stuck at every point of trying to fix that whether storing the text as another variable entirely or whatever. This is using firebase as it's backend, so maybe this is just a new quirk in irebase not to allow this.

package io.github.anoobishnoob.barker;

import android.content.Intent;
import android.nfc.Tag;
import android.renderscript.Sampler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Logger;

public class RegistrationActivity extends AppCompatActivity {



    //TODO: fix nullpointer eexception on line 64-67
    private Button mRegister;

    private EditText mEmail, mPassword, mName;

    private RadioGroup mRadioGroup;

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthStateListener;

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

        mAuth = FirebaseAuth.getInstance();
        firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                if (user != null){
                    Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }
            }
        };


        mRegister = (Button) findViewById(R.id.register);

        mEmail = (EditText) findViewById(R.id.email);
        mPassword = (EditText) findViewById(R.id.password);
        mName = (EditText) findViewById(R.id.name);

        mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        Log.d("test debug mRadioGroup", mRadioGroup.toString());


        mRegister.setOnClickListener(new View.OnClickListener() {
            int selectId = mRadioGroup.getCheckedRadioButtonId();
            final RadioButton radioButton = (RadioButton) findViewById(selectId);


            //String radioButtonValue = String.valueOf(radioButton.getText());

            @Override
            public void onClick(View v) {
                final String email = mEmail.getText().toString();
                final String password = mPassword.getText().toString();
                final String name = mName.getText().toString();
                Log.d("test debug", mRadioGroup.toString());
                //Log.d("test debug", radioButton.toString());
                mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(RegistrationActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()) {
                            Toast.makeText(RegistrationActivity.this, "sign up error", Toast.LENGTH_SHORT);

                        }
                        else{
                            String userId = mAuth.getCurrentUser().getUid();
                            DatabaseReference currentUserDb = FirebaseDatabase
                                    .getInstance()
                                    .getReference()
                                    .child("Users")
                                    .child(radioButton.toString()).child(userId).child("name");
                            currentUserDb.setValue(name);
                        }

                    }
                });

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(firebaseAuthStateListener);

    }


    @Override
    protected void onStop() {
        super.onStop();
        mAuth.removeAuthStateListener(firebaseAuthStateListener);
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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="io.github.anoobishnoob.barker.RegistrationActivity"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="name"
            android:id="@+id/name"/>
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radioGroup">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"/>
        </RadioGroup>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="email"
            android:id="@+id/email"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="password"
            android:id="@+id/password"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="register"
            android:id="@+id/register"/>

    </LinearLayout>

Upvotes: 1

Views: 89

Answers (2)

Rahul Khurana
Rahul Khurana

Reputation: 8844

Firstly give id to both RadioButton.

And after that, you can use it like below

int rid = mRadioGroup.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(rid);
String radioText = rb.getText().toString();

Upvotes: 1

Thamim
Thamim

Reputation: 328

Instead of radioButton.toString() use radioButton.getText().toString();

Upvotes: 1

Related Questions