-1

I have an intent service that sends an intent, like this:

Intent result = new Intent("TEST_RESULT");
result.putExtra("MODE", "testing");
sendBroadcast(result);

in another application, the intent gets captured by a broadcast receiver, like this:

receiver registration:

IntentFilter testResultIntentFilter = new IntentFilter("TEST_RESULT");
TestResultReceiver receiver = new TestResultReceiver();
this.registerReceiver(receiver,testResultIntentFilter);

onReceive method in the receiver:

@Override
public void onReceive(Context context, Intent intent) {
    String message = intent.getStringExtra("MODE");
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}

this code fails on the getStringExtra call with null pointer exception, it's as if the putExtra method is ignored. I tried everything but nothing works.

gazzo
  • 182
  • 2
  • 11
  • 2
    `Intent result = new Intent` and then `intent.putStri`. Is it a typo or you have multiple intents? – Blackbelt Nov 21 '18 at 16:45
  • 1
    Please provide the complete stack trace. There's really no way that the `getStringExtra()` line is throwing an NPE. That `Intent` cannot be null, and `getStringExtra()` will _return_ null if there is no `String` extra with that key, but it won't throw an NPE there. – Mike M. Nov 21 '18 at 17:58
  • 1
    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) – Lekr0 Nov 21 '18 at 18:16
  • fixed the issue? – Jins Lukose Nov 22 '18 at 06:37

1 Answers1

1
intent.putExtra("MODE", "testing");

should be

result.putExtra("MODE", "testing");
Peter
  • 13
  • 2