2

I'm getting this null exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference

and I don't know why.

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setTheme(R.style.LoginRegister);
        setContentView(R.layout.activity_report);

final TextView infoid = (TextView) findViewById(R.id.idInfo);

        RadioGroup rg = (RadioGroup)findViewById(R.id.rg);
        final String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();

...

        Button sendButton = (Button) findViewById(R.id.button2);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = infoid.getText().toString(); //what is wrong?
                Log.w("message: ", radiovalue);
            }
        });


}

my xml

   <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/idInfo"
        android:hint="Add some extra information"
        android:layout_weight="0.09"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp" />

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

any ideas?

RGS
  • 4,062
  • 4
  • 31
  • 67

4 Answers4

3
  1. Make sure TextView idInfo is exist in your activity_report.xml
  2. Try declaring idInfo as global.

    ..............
    ....................
    
    TextView infoid;
    
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.activity_report);
    
        infoid = (TextView) findViewById(R.id.idInfo);
    
        .............
        ...................
    
        Button sendButton = (Button) findViewById(R.id.button2);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = infoid.getText().toString();
                ..........
                .................
            }
        });
    
    }
    

UPDATE:

To get selected radioButton text:

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

// get selected radioButton from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();

// find the radioButton by returned id
radioButton = (RadioButton) findViewById(selectedId);

// radioButton text
String radiovalue = radioButton.getText();

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • I noticed that the problem is in the radio button: `RadioGroup rg = (RadioGroup)findViewById(R.id.rg); final String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();` I delete this code and it works. but now I need another way to read the item user selected... thank you. – RGS Apr 12 '17 at 18:56
  • 1
    To get selected radio button text, try my updated answer. Hope it will work for you... – Ferdous Ahamed Apr 12 '17 at 19:03
0
if (radioGroup.getCheckedRadioButtonId() <= 0) {
    Toast.makeText(yourclass, "Choose Radio Button Please", Toast.LENGTH_SHORT).show();
}
Sebastian D'Agostino
  • 1,575
  • 2
  • 27
  • 44
0

If your are using RadioGroup inside Dialog / Alert Dialog then you have to cast it as

     RadioButton radioButton = dialog.findViewById(checkedId);
            Toast.makeText(ReLoan_Activity.this, "Selected Radio Button is 
           : " + radioButton.getText().toString(), 
            Toast.LENGTH_SHORT).show();
        
Rakesh Jha
  • 279
  • 2
  • 7
-1

check this

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

  private RadioGroup radioGroup;
  private RadioButton radioButton;
  private Button btnDisplay;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

  }

  public void addListenerOnButton() {

    radioGroup = (RadioGroup) findViewById(R.id.radio);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);

    btnDisplay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                // get selected radio button from radioGroup
            int selectedId = radioGroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
            radioButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioButton.getText(), Toast.LENGTH_SHORT).show();

        }

    });

  }
}
  • What is the meaning of this link on the start of your question? It is a link to an answer in another StackOverflow but what is the relation with your question? You could describe better your question. You are receiving answers just because you've successfully posted your code and the error is a basic one but it would be better to place the description in the body and not in the title itself and you could organize better your post in order to be more clear. – Iogui May 14 '21 at 14:59