Why does WindowManager.getdefaultdisplay() warn of a null pointer exception?












0















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<>();









share|improve this question



























    0















    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<>();









    share|improve this question

























      0












      0








      0








      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<>();









      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 18:11









      EndOfAllEndOfAll

      829




      829
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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
          }





          share|improve this answer
























          • Why does Android Studio hide the warning when I add a line between the two?

            – EndOfAll
            Nov 20 '18 at 18:35






          • 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











          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          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
          }





          share|improve this answer
























          • Why does Android Studio hide the warning when I add a line between the two?

            – EndOfAll
            Nov 20 '18 at 18:35






          • 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











          • 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
















          1














          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
          }





          share|improve this answer
























          • Why does Android Studio hide the warning when I add a line between the two?

            – EndOfAll
            Nov 20 '18 at 18:35






          • 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











          • 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














          1












          1








          1







          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
          }





          share|improve this answer













          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
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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





            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











          • 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






          • 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











          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()