-1

My MainActivity.class is invoking putExtra towards SecondActivity.class via setOnClickListener

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("sentString", stringName);
startActivity(intent);

SecondActivity.class setOnClickListener

//    Recieve the extra sent from MainActivity.class
Intent SecondActivityIntent = getIntent();
String mString = SecondActivityIntent .getExtras().getString("sentString");

//    Send extra to another activity ThirdActivity.class
SecondActivityIntent.putExtra("sentString", mString);
startActivity(SecondActivityIntent); 

ThirdActivity.class setOnClickListener

//    Recieve extra from SecondActivity.class
Intent thirdActivityintent = getIntent();
String mString = thirdActivityintent.getExtra().getString("sentString");
//    This time I am calling SecondActivity.class but I will not send extra
thirdActivityintent = new Intent(ThirdActivity.this, SecondActivity.class);
startActivity(thirdActivityintent);

ThirdActivity.class is causing an

'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

what does this error mean? Is it because SecondActivity.class is expecting to get an extra from any calling activity? I don't intend to putExtra on ThirdActivity or am I force to. How can this be solve?

Principiante
  • 131
  • 1
  • 2
  • 8

3 Answers3

2

Is it because SecondActivity.class is expecting to get and extra from any calling activity?

Yes.

How can this be solve?

Put a default value and a null check

//    Receive the extra sent from MainActivity.class
Bundle extras = getIntent().getExtras();
String mString = "default";

if (extras != null) {
    mString = extras.getString("sentString");
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Can't apply this because the value of the mString will be provided by the end-user – Principiante Aug 11 '17 at 02:47
  • The end user is running in an interactive session... The point I am trying to make here is that you have a null or default value when the Intent does not exist – OneCricketeer Aug 11 '17 at 02:55
  • Thanks for sharing. This will be helpful on my future apps. Yet in my current app, I cannot apply it. finish() does the trick but only if my app goes back 1 step but if I want to back further on the activity stack, finish() won't be able to do it, at least to my knowledge. – Principiante Aug 11 '17 at 04:21
1

Change it into like this FirstActivity

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("sentString", stringName);
startActivity(intent);

In SecondActivity

//    Recieve the extra sent from MainActivity.class
Intent SecondActivityIntent = getIntent();
String mString = SecondActivityIntent .getStringExtra("sentString");
//    Send extra to another activity ThirdActivity.class
Intent thirdIntent = new Intent(SecondActivity.this, ThirdActivity.class);
thirdIntent.putExtra("sentString", mString);
startActivity(thirdIntent); 

The ThirdActivity

//    Recieve extra from SecondActivity.class
Intent thirdActivityintent = getIntent();
String mString = thirdActivityintent.getStringExtra("sentString");
//    Just finish Activity
finish();
Fr099y
  • 734
  • 7
  • 15
0

Change

.getExtras().getString("sentString");

to

.getExtras().getString("sentString", "defaultValue");
Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22