0

I have two RadioButtons within a RadioGroup. I also set up a Button, with a setOnClickListener function. When the button is pressed, its supposed to set the float variable gender to a specific value depending on what RadioButton is selected. However, whenever the button is pressed the app crashes.

I've tried setting up a TextView just to see if it would print anything, but the app just crashes when the button is pressed. I looked into the Logcat, and saw that it threw this error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference

package com.example.bacwatch;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ProfileFragment extends Fragment {
    private EditText editWeight;
    private Button buttonConfirm;

    public float gender;
    RadioGroup radioGroup;
    RadioButton radioButton;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_profile, container, false);

        radioGroup = v.findViewById(R.id.gender_buttons);
        editWeight = v.findViewById(R.id.edit_Weight);
        buttonConfirm = v.findViewById(R.id.confirm_button);

        editWeight.addTextChangedListener(loginTextWatcher);

        // Button will input text.
        buttonConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Records the gender from the selected radio button.
                int radioID = radioGroup.getCheckedRadioButtonId();
                radioButton = v.findViewById(radioID);
                if (radioButton.getText().equals("Male")) {
                    gender = (float) .58;
                } else {
                    gender = (float) .49;
                }


                // Records User's weight (in pounds) and stores it into a variable.
                /*float weight = Float.valueOf(editWeight.getText().toString().trim()).floatValue();
                // Converts pounds to kilograms.
                float kilo_weight = (float) (weight/2.2046);
                // Calculates total body water.
                float body_water = kilo_weight*gender;*/

                // Clears text entries.
                editWeight.getText().clear();
            }
        });

        return v;
    }

    private TextWatcher loginTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String weightInput = editWeight.getText().toString().trim();

            buttonConfirm.setEnabled(!weightInput.isEmpty());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
}

XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@android:color/holo_green_light"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender:"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
        android:id="@+id/gender_text"/>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/gender_text"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        android:id="@+id/gender_buttons">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male"
                android:checked="true"
                android:textSize="30sp"
                android:id="@+id/male_button"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Female"
                android:textSize="30sp"
                android:id="@+id/female_button"/>
    </RadioGroup>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/gender_buttons"
        android:id="@+id/weight_box">

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/weight_input">

            <android.support.design.widget.TextInputEditText
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:hint="Weight in Pounds"
                android:textSize="30sp"
                android:inputType="number"
                android:id="@+id/edit_Weight"/>
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/weight_box"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        android:text="Confirm"
        android:enabled="false"
        android:id="@+id/confirm_button"/>

</RelativeLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mark
  • 13
  • 2
  • 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) – Zoe May 15 '19 at 15:30

1 Answers1

1

the app crashes because you tring to get radioButtion from the view that handling the click and not your layout view

radioButton = v.findViewById(radioID);

you have to use another view name to set a difference between them

buttonConfirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Records the gender from the selected radio button.
            int radioID = radioGroup.getCheckedRadioButtonId();
            radioButton = v.findViewById(radioID);
            if (radioButton.getText().equals("Male")) {
                gender = (float) .58;
            } else {
                gender = (float) .49;
            }
         ......
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38