null pointer exception on getStringExtra
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.



add a comment |
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.



2
Intent result = new Intent
and thenintent.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 thegetStringExtra()
line is throwing an NPE. ThatIntent
cannot be null, andgetStringExtra()
will return null if there is noString
extra with that key, but it won't throw an NPE there.
– Mike M.
Nov 21 '18 at 17:58
Possible duplicate of 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 6:37
add a comment |
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.



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.






edited Nov 21 '18 at 16:50
user1534252
asked Nov 21 '18 at 16:42
user1534252user1534252
1615
1615
2
Intent result = new Intent
and thenintent.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 thegetStringExtra()
line is throwing an NPE. ThatIntent
cannot be null, andgetStringExtra()
will return null if there is noString
extra with that key, but it won't throw an NPE there.
– Mike M.
Nov 21 '18 at 17:58
Possible duplicate of 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 6:37
add a comment |
2
Intent result = new Intent
and thenintent.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 thegetStringExtra()
line is throwing an NPE. ThatIntent
cannot be null, andgetStringExtra()
will return null if there is noString
extra with that key, but it won't throw an NPE there.
– Mike M.
Nov 21 '18 at 17:58
Possible duplicate of 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 6:37
2
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
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
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
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
Possible duplicate of What is a NullPointerException, and how do I fix it?
– Lekr0
Nov 21 '18 at 18:16
Possible duplicate of 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 6:37
fixed the issue?
– Jins Lukose
Nov 22 '18 at 6:37
add a comment |
1 Answer
1
active
oldest
votes
intent.putExtra("MODE", "testing");
should be
result.putExtra("MODE", "testing");
sorry, my mistake in writing the question
– user1534252
Nov 21 '18 at 16:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416799%2fnull-pointer-exception-on-getstringextra%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
intent.putExtra("MODE", "testing");
should be
result.putExtra("MODE", "testing");
sorry, my mistake in writing the question
– user1534252
Nov 21 '18 at 16:50
add a comment |
intent.putExtra("MODE", "testing");
should be
result.putExtra("MODE", "testing");
sorry, my mistake in writing the question
– user1534252
Nov 21 '18 at 16:50
add a comment |
intent.putExtra("MODE", "testing");
should be
result.putExtra("MODE", "testing");
intent.putExtra("MODE", "testing");
should be
result.putExtra("MODE", "testing");
answered Nov 21 '18 at 16:48
PeterPeter
132
132
sorry, my mistake in writing the question
– user1534252
Nov 21 '18 at 16:50
add a comment |
sorry, my mistake in writing the question
– user1534252
Nov 21 '18 at 16:50
sorry, my mistake in writing the question
– user1534252
Nov 21 '18 at 16:50
sorry, my mistake in writing the question
– user1534252
Nov 21 '18 at 16:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416799%2fnull-pointer-exception-on-getstringextra%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Intent result = new Intent
and thenintent.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. ThatIntent
cannot be null, andgetStringExtra()
will return null if there is noString
extra with that key, but it won't throw an NPE there.– Mike M.
Nov 21 '18 at 17:58
Possible duplicate of 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 6:37