How do I fix this dereference of nullable error?











up vote
1
down vote

favorite












I've been trying to get rid of the dereference of a possible null-reference graph for the longest time, but nothing seems to work.



This is my original code:



if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}


where pluto is private final Map> pluto;



and I have also tried



if (pluto.get(boxer.getName()) != null) {
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
}


But this also doesn't work. Please help.



[dereference.of.nullable] dereference of possibly-null reference pluto.get(boxer.getName())










share|improve this question






















  • What pluto.get(boxer.getName()) returns?
    – efex09
    Nov 8 at 5:44












  • can you please share full code?
    – GauravRai1512
    Nov 8 at 5:45










  • it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
    – Adrian Markoe
    Nov 8 at 5:45










  • There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
    – GauravRai1512
    Nov 8 at 5:50










  • You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
    – cokeby190
    Nov 8 at 9:39















up vote
1
down vote

favorite












I've been trying to get rid of the dereference of a possible null-reference graph for the longest time, but nothing seems to work.



This is my original code:



if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}


where pluto is private final Map> pluto;



and I have also tried



if (pluto.get(boxer.getName()) != null) {
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
}


But this also doesn't work. Please help.



[dereference.of.nullable] dereference of possibly-null reference pluto.get(boxer.getName())










share|improve this question






















  • What pluto.get(boxer.getName()) returns?
    – efex09
    Nov 8 at 5:44












  • can you please share full code?
    – GauravRai1512
    Nov 8 at 5:45










  • it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
    – Adrian Markoe
    Nov 8 at 5:45










  • There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
    – GauravRai1512
    Nov 8 at 5:50










  • You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
    – cokeby190
    Nov 8 at 9:39













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I've been trying to get rid of the dereference of a possible null-reference graph for the longest time, but nothing seems to work.



This is my original code:



if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}


where pluto is private final Map> pluto;



and I have also tried



if (pluto.get(boxer.getName()) != null) {
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
}


But this also doesn't work. Please help.



[dereference.of.nullable] dereference of possibly-null reference pluto.get(boxer.getName())










share|improve this question













I've been trying to get rid of the dereference of a possible null-reference graph for the longest time, but nothing seems to work.



This is my original code:



if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}


where pluto is private final Map> pluto;



and I have also tried



if (pluto.get(boxer.getName()) != null) {
if (!pluto.get(boxer.getName()).contains(boxer)) {
pluto.get(boxer.getName()).add(boxer);
}
}


But this also doesn't work. Please help.



[dereference.of.nullable] dereference of possibly-null reference pluto.get(boxer.getName())







java null dereference






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 5:40









Adrian Markoe

155




155












  • What pluto.get(boxer.getName()) returns?
    – efex09
    Nov 8 at 5:44












  • can you please share full code?
    – GauravRai1512
    Nov 8 at 5:45










  • it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
    – Adrian Markoe
    Nov 8 at 5:45










  • There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
    – GauravRai1512
    Nov 8 at 5:50










  • You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
    – cokeby190
    Nov 8 at 9:39


















  • What pluto.get(boxer.getName()) returns?
    – efex09
    Nov 8 at 5:44












  • can you please share full code?
    – GauravRai1512
    Nov 8 at 5:45










  • it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
    – Adrian Markoe
    Nov 8 at 5:45










  • There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
    – GauravRai1512
    Nov 8 at 5:50










  • You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
    – cokeby190
    Nov 8 at 9:39
















What pluto.get(boxer.getName()) returns?
– efex09
Nov 8 at 5:44






What pluto.get(boxer.getName()) returns?
– efex09
Nov 8 at 5:44














can you please share full code?
– GauravRai1512
Nov 8 at 5:45




can you please share full code?
– GauravRai1512
Nov 8 at 5:45












it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
– Adrian Markoe
Nov 8 at 5:45




it returns the set of strings that it is mapped to. I don't know why, but stack overflow removed my implementation for my graph. it is private final Map<String, Set<String>> pluto
– Adrian Markoe
Nov 8 at 5:45












There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
– GauravRai1512
Nov 8 at 5:50




There might be two possibility one map is null and another possibility is boxer.getName is null also you can verify by run your code is debug mode so you can inspect the element and check what is getting null.
– GauravRai1512
Nov 8 at 5:50












You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
– cokeby190
Nov 8 at 9:39




You would need to check if pluto is null or if boxer is null before calling "if (pluto.get(boxer.getName()) != null)". If any of them is null it will throw a NullpointerException.
– cokeby190
Nov 8 at 9:39












2 Answers
2






active

oldest

votes

















up vote
0
down vote













What if your map is null?



try this:



if (isNull(pluto)) { // import static java.util.Objects.isNull;
return; // to minimise nesting
}

Optional.ofNullable(pluto.get(boxer.getName()))
.filter(value -> !value.isEmpty())
.ifPresent(value -> { /*do whatever you want*/ });





share|improve this answer






























    up vote
    0
    down vote













    Try this. You need to check for nulls.



    if (pluto != null && pluto.get(boxer.getName()) != null && !pluto.get(boxer.getName()).contains(boxer)) {
    pluto.get(boxer.getName()).add(boxer);
    }





    share|improve this answer





















      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',
      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%2f53202104%2fhow-do-i-fix-this-dereference-of-nullable-error%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      What if your map is null?



      try this:



      if (isNull(pluto)) { // import static java.util.Objects.isNull;
      return; // to minimise nesting
      }

      Optional.ofNullable(pluto.get(boxer.getName()))
      .filter(value -> !value.isEmpty())
      .ifPresent(value -> { /*do whatever you want*/ });





      share|improve this answer



























        up vote
        0
        down vote













        What if your map is null?



        try this:



        if (isNull(pluto)) { // import static java.util.Objects.isNull;
        return; // to minimise nesting
        }

        Optional.ofNullable(pluto.get(boxer.getName()))
        .filter(value -> !value.isEmpty())
        .ifPresent(value -> { /*do whatever you want*/ });





        share|improve this answer

























          up vote
          0
          down vote










          up vote
          0
          down vote









          What if your map is null?



          try this:



          if (isNull(pluto)) { // import static java.util.Objects.isNull;
          return; // to minimise nesting
          }

          Optional.ofNullable(pluto.get(boxer.getName()))
          .filter(value -> !value.isEmpty())
          .ifPresent(value -> { /*do whatever you want*/ });





          share|improve this answer














          What if your map is null?



          try this:



          if (isNull(pluto)) { // import static java.util.Objects.isNull;
          return; // to minimise nesting
          }

          Optional.ofNullable(pluto.get(boxer.getName()))
          .filter(value -> !value.isEmpty())
          .ifPresent(value -> { /*do whatever you want*/ });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 8 at 5:55

























          answered Nov 8 at 5:47









          Abhishek

          337114




          337114
























              up vote
              0
              down vote













              Try this. You need to check for nulls.



              if (pluto != null && pluto.get(boxer.getName()) != null && !pluto.get(boxer.getName()).contains(boxer)) {
              pluto.get(boxer.getName()).add(boxer);
              }





              share|improve this answer

























                up vote
                0
                down vote













                Try this. You need to check for nulls.



                if (pluto != null && pluto.get(boxer.getName()) != null && !pluto.get(boxer.getName()).contains(boxer)) {
                pluto.get(boxer.getName()).add(boxer);
                }





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Try this. You need to check for nulls.



                  if (pluto != null && pluto.get(boxer.getName()) != null && !pluto.get(boxer.getName()).contains(boxer)) {
                  pluto.get(boxer.getName()).add(boxer);
                  }





                  share|improve this answer












                  Try this. You need to check for nulls.



                  if (pluto != null && pluto.get(boxer.getName()) != null && !pluto.get(boxer.getName()).contains(boxer)) {
                  pluto.get(boxer.getName()).add(boxer);
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 5:56









                  efex09

                  1809




                  1809






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53202104%2fhow-do-i-fix-this-dereference-of-nullable-error%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







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings