Why does WindowManager.getdefaultdisplay() warn of a null pointer exception?
Below is some code I have in Android Studio. Android Studio warns that getdefaultdisplay() may throw a null pointer exception.
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
display = windowManager.getDefaultDisplay();
layoutInflater = LayoutInflater.from(context);
actions = new ArrayList<>();
However, when I flip line 3 and 2... it no longer warns. The lines are unrelated though... why is this the case?
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
layoutInflater = LayoutInflater.from(context);
display = windowManager.getDefaultDisplay();
actions = new ArrayList<>();
android android-studio
add a comment |
Below is some code I have in Android Studio. Android Studio warns that getdefaultdisplay() may throw a null pointer exception.
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
display = windowManager.getDefaultDisplay();
layoutInflater = LayoutInflater.from(context);
actions = new ArrayList<>();
However, when I flip line 3 and 2... it no longer warns. The lines are unrelated though... why is this the case?
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
layoutInflater = LayoutInflater.from(context);
display = windowManager.getDefaultDisplay();
actions = new ArrayList<>();
android android-studio
add a comment |
Below is some code I have in Android Studio. Android Studio warns that getdefaultdisplay() may throw a null pointer exception.
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
display = windowManager.getDefaultDisplay();
layoutInflater = LayoutInflater.from(context);
actions = new ArrayList<>();
However, when I flip line 3 and 2... it no longer warns. The lines are unrelated though... why is this the case?
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
layoutInflater = LayoutInflater.from(context);
display = windowManager.getDefaultDisplay();
actions = new ArrayList<>();
android android-studio
Below is some code I have in Android Studio. Android Studio warns that getdefaultdisplay() may throw a null pointer exception.
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
display = windowManager.getDefaultDisplay();
layoutInflater = LayoutInflater.from(context);
actions = new ArrayList<>();
However, when I flip line 3 and 2... it no longer warns. The lines are unrelated though... why is this the case?
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
layoutInflater = LayoutInflater.from(context);
display = windowManager.getDefaultDisplay();
actions = new ArrayList<>();
android android-studio
android android-studio
asked Nov 20 '18 at 18:11
EndOfAllEndOfAll
829
829
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Any method that warns NullPointerException
are marked as @Nullable
, which means that it might return null
, so you need to check the result of the getDefaultDisplay()
method before using it in order to be sure that the code will not crash because of NullPointerException
Display display = windowManager.getDefaultDisplay()
if(display != null) {
// do what you need here
}
Why does Android Studio hide the warning when I add a line between the two?
– EndOfAll
Nov 20 '18 at 18:35
1
becauseLayoutInflater.from(context)
will callgetSystemService(...)
which is called in getting the windowManager, and this method if did not return null, then android studio will be sure that the first line did not return null because it's the same method.
– Khalid Taha
Nov 20 '18 at 18:42
That's exactly what I was looking for. Thanks Khalid.
– EndOfAll
Nov 20 '18 at 18:43
Welcome, Any Time :)
– Khalid Taha
Nov 20 '18 at 18:44
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%2f53399032%2fwhy-does-windowmanager-getdefaultdisplay-warn-of-a-null-pointer-exception%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
Any method that warns NullPointerException
are marked as @Nullable
, which means that it might return null
, so you need to check the result of the getDefaultDisplay()
method before using it in order to be sure that the code will not crash because of NullPointerException
Display display = windowManager.getDefaultDisplay()
if(display != null) {
// do what you need here
}
Why does Android Studio hide the warning when I add a line between the two?
– EndOfAll
Nov 20 '18 at 18:35
1
becauseLayoutInflater.from(context)
will callgetSystemService(...)
which is called in getting the windowManager, and this method if did not return null, then android studio will be sure that the first line did not return null because it's the same method.
– Khalid Taha
Nov 20 '18 at 18:42
That's exactly what I was looking for. Thanks Khalid.
– EndOfAll
Nov 20 '18 at 18:43
Welcome, Any Time :)
– Khalid Taha
Nov 20 '18 at 18:44
add a comment |
Any method that warns NullPointerException
are marked as @Nullable
, which means that it might return null
, so you need to check the result of the getDefaultDisplay()
method before using it in order to be sure that the code will not crash because of NullPointerException
Display display = windowManager.getDefaultDisplay()
if(display != null) {
// do what you need here
}
Why does Android Studio hide the warning when I add a line between the two?
– EndOfAll
Nov 20 '18 at 18:35
1
becauseLayoutInflater.from(context)
will callgetSystemService(...)
which is called in getting the windowManager, and this method if did not return null, then android studio will be sure that the first line did not return null because it's the same method.
– Khalid Taha
Nov 20 '18 at 18:42
That's exactly what I was looking for. Thanks Khalid.
– EndOfAll
Nov 20 '18 at 18:43
Welcome, Any Time :)
– Khalid Taha
Nov 20 '18 at 18:44
add a comment |
Any method that warns NullPointerException
are marked as @Nullable
, which means that it might return null
, so you need to check the result of the getDefaultDisplay()
method before using it in order to be sure that the code will not crash because of NullPointerException
Display display = windowManager.getDefaultDisplay()
if(display != null) {
// do what you need here
}
Any method that warns NullPointerException
are marked as @Nullable
, which means that it might return null
, so you need to check the result of the getDefaultDisplay()
method before using it in order to be sure that the code will not crash because of NullPointerException
Display display = windowManager.getDefaultDisplay()
if(display != null) {
// do what you need here
}
answered Nov 20 '18 at 18:31
Khalid TahaKhalid Taha
1,89211531
1,89211531
Why does Android Studio hide the warning when I add a line between the two?
– EndOfAll
Nov 20 '18 at 18:35
1
becauseLayoutInflater.from(context)
will callgetSystemService(...)
which is called in getting the windowManager, and this method if did not return null, then android studio will be sure that the first line did not return null because it's the same method.
– Khalid Taha
Nov 20 '18 at 18:42
That's exactly what I was looking for. Thanks Khalid.
– EndOfAll
Nov 20 '18 at 18:43
Welcome, Any Time :)
– Khalid Taha
Nov 20 '18 at 18:44
add a comment |
Why does Android Studio hide the warning when I add a line between the two?
– EndOfAll
Nov 20 '18 at 18:35
1
becauseLayoutInflater.from(context)
will callgetSystemService(...)
which is called in getting the windowManager, and this method if did not return null, then android studio will be sure that the first line did not return null because it's the same method.
– Khalid Taha
Nov 20 '18 at 18:42
That's exactly what I was looking for. Thanks Khalid.
– EndOfAll
Nov 20 '18 at 18:43
Welcome, Any Time :)
– Khalid Taha
Nov 20 '18 at 18:44
Why does Android Studio hide the warning when I add a line between the two?
– EndOfAll
Nov 20 '18 at 18:35
Why does Android Studio hide the warning when I add a line between the two?
– EndOfAll
Nov 20 '18 at 18:35
1
1
because
LayoutInflater.from(context)
will call getSystemService(...)
which is called in getting the windowManager, and this method if did not return null, then android studio will be sure that the first line did not return null because it's the same method.– Khalid Taha
Nov 20 '18 at 18:42
because
LayoutInflater.from(context)
will call getSystemService(...)
which is called in getting the windowManager, and this method if did not return null, then android studio will be sure that the first line did not return null because it's the same method.– Khalid Taha
Nov 20 '18 at 18:42
That's exactly what I was looking for. Thanks Khalid.
– EndOfAll
Nov 20 '18 at 18:43
That's exactly what I was looking for. Thanks Khalid.
– EndOfAll
Nov 20 '18 at 18:43
Welcome, Any Time :)
– Khalid Taha
Nov 20 '18 at 18:44
Welcome, Any Time :)
– Khalid Taha
Nov 20 '18 at 18:44
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%2f53399032%2fwhy-does-windowmanager-getdefaultdisplay-warn-of-a-null-pointer-exception%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