-2

I've been looking into this for a few days and can't seem to find a thread specific to my problem. My app crashes every time I try to add a string to my bundle. I'm getting the error at extras.putString(EXTRA_MESSAGE, message);

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> priceGroupSpinnerItems;
    public static final String EXTRA_MESSAGE = "lss.mywebsiteoffline.MESSAGE";
    public String message = "";

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

        Spinner spnPriceGroup = (Spinner) findViewById(R.id.price_group_spinner);

        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.list_price_group, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spnPriceGroup.setAdapter(adapter);

        message = spnPriceGroup.getSelectedItem().toString();
    }

    /** Called when the user taps the Go button */
    public void sendMessage(View view) {

        Intent mIntent;

        mIntent = new Intent(this, nextActivity.class);
        Bundle extras = mIntent.getExtras();
        extras.putString(EXTRA_MESSAGE, message);
        mIntent.putExtras(extras);
        startActivity(mIntent);

    }
}

Logcat

E/AndroidRuntime: FATAL EXCEPTION: main
Process: lss.mybbofflinever3, PID: 8181
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5702)
at android.widget.TextView.performClick(TextView.java:10896)
at android.view.View$PerformClick.run(View.java:22546)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5702) 
at android.widget.TextView.performClick(TextView.java:10896) 
at android.view.View$PerformClick.run(View.java:22546) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Bundle.putString(java.lang.String, java.lang.String)' on a null object reference
at lss.mywebsiteoffline.MainActivity.sendMessage(MainActivity.java:59)

I have tried examples to use just intent, or bundle, and adding strings directly to putString. I might be missing something blatantly obvious. Not sure what I might be looking for after adding strings directly to the putString method and getting the same error.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • `extras` might be `null`, did you check it? – Nir Alfasi Oct 28 '17 at 16:00
  • @alfasin: I ran into that article a few times already. It wasn't quite clear (to me at least) but laalto's answer seemed to fix my problem. Not sure if the article would have led me to the same answer though.. – A. Callender Oct 28 '17 at 16:29

2 Answers2

1

By default an Intent does not have extras and getExtras() returns null. If you want to set extras, you need to create the extras bundle yourself. Replace

Bundle extras = mIntent.getExtras();

with

Bundle extras = new Bundle();

Or just call one of the putExtra() overloads on the Intent - it will also init the extras bundle in case it's not there already.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Try this:

Intent mIntent = new Intent(this, nextActivity.class);
mIntent.putExtra(EXTRA_MESSAGE, message);
startActivity(mIntent);
Shunt
  • 179
  • 8