0

I making a fitness application in Android Studio and my projcet has Multiple Activities (6). I am trying to get the value of the radiogroup from the ThirdActivity in the SixthActivity. However, when I try to do this, I get the error:

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

I have made sure that all the names for the variables in the xml code are correct and am still running into the same problem.

SixthActivity.java

  Intent intent=getIntent();
  String experience =intent.getStringExtra("Experience");

I would like to be able to get the value from thirdRadioGroup RadioGroup in ThirdActivity while being in the SixthActivity.

new code:

//ThirdActivity.class

 thirdRadioGroup = findViewById(R.id.thirdRadioGroup);
 int radioIdExperience = thirdRadioGroup.getCheckedRadioButtonId();
 experience = (RadioButton) findViewById(radioIdExperience);
 Intent intent = new Intent(this, SixthActivity.class);
 intent.putExtra("Experience", experience.getText());
 startActivity(intent);

Now, again this throw NullPointerException..

error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference
Vikrant Kashyap
  • 5,028
  • 1
  • 23
  • 46
FranceMadrid
  • 95
  • 1
  • 10
  • And a repost of https://stackoverflow.com/questions/55321473/null-object-reference-on-radiogroup-variable-android-studio – Zoe Mar 28 '19 at 07:52

2 Answers2

1

You are getting the null pointer exception because the ThirdActivity is paused. ThirdActivity is paused because you have the Sixth one running. One way to get your data from the ThirdActivity is to pass them as extra intent values when navigating between them. Example Java, you do this on the ThirdActivity

Intent intent = new Intent(ThirdActivity .this, SixthActivity.class);
intent.putExtra("Experience",experience.getText());
startActivity(intent);

In your sixth activity you receive the data like this:

Intent intent=getIntent();
String experience =intent.getStringExtra("Experience");
bensadiku
  • 432
  • 3
  • 14
  • Would I need to pass the radiogroup value as an intent in ThirdActivity? – FranceMadrid Mar 27 '19 at 22:37
  • Yes, that is one way to do it. – Kars Mar 27 '19 at 22:37
  • @Kars Could you provide any pseudocode on how to do this? Anything would be greatly appreciated – FranceMadrid Mar 27 '19 at 22:39
  • Edited to provide a pseudocode example. – bensadiku Mar 27 '19 at 22:40
  • @bensadiku Thank you the pseudocode helps a lot. However, I am getting an error on the line intent.putExtra("Experience",experience.getText());. The error states : java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference. In that line you state experience.getText() in ThirdActivity.java, but I created the experience button in the Sixth Activity. Am I to create the experience button in the thirdActivity now? I did that and received the error above. – FranceMadrid Mar 27 '19 at 22:49
  • What's the error? I just copied from your text. – bensadiku Mar 27 '19 at 22:50
  • @bensadiku I edited my comment to be more thorough – FranceMadrid Mar 27 '19 at 22:51
  • So my pseudo code assumes your radio buttons exist in the ThirdActivity only. Therefore the code `experience.getText()` is only meant to be ran on the Third Activity only. You should remove the old attempts/buttons from the sixth activity because as we discussed that is not possible and will produce null pointer exception. – bensadiku Mar 27 '19 at 22:53
  • @bensadiku That is what I did. I edited my original post to add the code you suggested if you want to take a look. – FranceMadrid Mar 27 '19 at 22:56
  • Two things: 1) Your edit doesn't reflect that you removed the radio button code from the sixth activity. 2) Does the radio button with the id `radioIdExperience` exist in the third activity? Is it being inflated properly? – bensadiku Mar 27 '19 at 22:59
  • @bensadiku I edited the original post. Sorry. And no, I don't have a radiobutton called radioIdExperience, that is an integer that holds the value of the radioGroup, isnt it? – FranceMadrid Mar 27 '19 at 23:02
  • Yes, each button needs to have an id `experience = (RadioButton) findViewById(radioIdExperience);` this is wrong. Should be `experience = (RadioButton) findViewById(R.id.radioIdExperience);` – bensadiku Mar 27 '19 at 23:04
0

One way to pass values between activities is by using putExtra on the intent responsible for the new activity.

intent.putExtra("NAME", value)

You can then access the value by calling getExtra on the new activity.

getIntent().getIntExtra("NAME")
getIntent().getStringExtra("NAME")

https://developer.android.com/reference/android/content/Intent

Kars
  • 617
  • 1
  • 7
  • 21