Kotlin Incorrect null check warning












-1















class A
{
val b:B
val at:String
init
{
b=B(this)
at="A's text"
}
}

class B(a:A)
{
val bt:String
init
{
bt= if(a.at!=null) a.at.replaceFirst("A's","B's") else "else's text"
}

}


This code will generate a warning




Condition 'a.at!=null' is always 'true'




but actually the condition 'a.at!=null' is always false.










share|improve this question

























  • a.at's type is String, which is non-null (hint: String? is nullable, String is non-null). So what do you mean by "it is always false"?

    – Geno Chen
    Nov 24 '18 at 6:47











  • Even a.at is non-null, the condition a.at!=null will be always false. Check the output of this code link

    – poocharali
    Nov 24 '18 at 7:00













  • Oh you mean this. The results depends on the sequence. If the two lines in init of class A swaps, then the result will be expected. I wonder if this is a bug in grammar analysis.

    – Geno Chen
    Nov 24 '18 at 7:05











  • Is there any particular reason why you initialize all properties in init block instead of in-place? Though this doesn't affect this question.

    – Alexey Romanov
    Nov 24 '18 at 9:48
















-1















class A
{
val b:B
val at:String
init
{
b=B(this)
at="A's text"
}
}

class B(a:A)
{
val bt:String
init
{
bt= if(a.at!=null) a.at.replaceFirst("A's","B's") else "else's text"
}

}


This code will generate a warning




Condition 'a.at!=null' is always 'true'




but actually the condition 'a.at!=null' is always false.










share|improve this question

























  • a.at's type is String, which is non-null (hint: String? is nullable, String is non-null). So what do you mean by "it is always false"?

    – Geno Chen
    Nov 24 '18 at 6:47











  • Even a.at is non-null, the condition a.at!=null will be always false. Check the output of this code link

    – poocharali
    Nov 24 '18 at 7:00













  • Oh you mean this. The results depends on the sequence. If the two lines in init of class A swaps, then the result will be expected. I wonder if this is a bug in grammar analysis.

    – Geno Chen
    Nov 24 '18 at 7:05











  • Is there any particular reason why you initialize all properties in init block instead of in-place? Though this doesn't affect this question.

    – Alexey Romanov
    Nov 24 '18 at 9:48














-1












-1








-1








class A
{
val b:B
val at:String
init
{
b=B(this)
at="A's text"
}
}

class B(a:A)
{
val bt:String
init
{
bt= if(a.at!=null) a.at.replaceFirst("A's","B's") else "else's text"
}

}


This code will generate a warning




Condition 'a.at!=null' is always 'true'




but actually the condition 'a.at!=null' is always false.










share|improve this question
















class A
{
val b:B
val at:String
init
{
b=B(this)
at="A's text"
}
}

class B(a:A)
{
val bt:String
init
{
bt= if(a.at!=null) a.at.replaceFirst("A's","B's") else "else's text"
}

}


This code will generate a warning




Condition 'a.at!=null' is always 'true'




but actually the condition 'a.at!=null' is always false.







kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 13:33







poocharali

















asked Nov 24 '18 at 6:38









poocharalipoocharali

14




14













  • a.at's type is String, which is non-null (hint: String? is nullable, String is non-null). So what do you mean by "it is always false"?

    – Geno Chen
    Nov 24 '18 at 6:47











  • Even a.at is non-null, the condition a.at!=null will be always false. Check the output of this code link

    – poocharali
    Nov 24 '18 at 7:00













  • Oh you mean this. The results depends on the sequence. If the two lines in init of class A swaps, then the result will be expected. I wonder if this is a bug in grammar analysis.

    – Geno Chen
    Nov 24 '18 at 7:05











  • Is there any particular reason why you initialize all properties in init block instead of in-place? Though this doesn't affect this question.

    – Alexey Romanov
    Nov 24 '18 at 9:48



















  • a.at's type is String, which is non-null (hint: String? is nullable, String is non-null). So what do you mean by "it is always false"?

    – Geno Chen
    Nov 24 '18 at 6:47











  • Even a.at is non-null, the condition a.at!=null will be always false. Check the output of this code link

    – poocharali
    Nov 24 '18 at 7:00













  • Oh you mean this. The results depends on the sequence. If the two lines in init of class A swaps, then the result will be expected. I wonder if this is a bug in grammar analysis.

    – Geno Chen
    Nov 24 '18 at 7:05











  • Is there any particular reason why you initialize all properties in init block instead of in-place? Though this doesn't affect this question.

    – Alexey Romanov
    Nov 24 '18 at 9:48

















a.at's type is String, which is non-null (hint: String? is nullable, String is non-null). So what do you mean by "it is always false"?

– Geno Chen
Nov 24 '18 at 6:47





a.at's type is String, which is non-null (hint: String? is nullable, String is non-null). So what do you mean by "it is always false"?

– Geno Chen
Nov 24 '18 at 6:47













Even a.at is non-null, the condition a.at!=null will be always false. Check the output of this code link

– poocharali
Nov 24 '18 at 7:00







Even a.at is non-null, the condition a.at!=null will be always false. Check the output of this code link

– poocharali
Nov 24 '18 at 7:00















Oh you mean this. The results depends on the sequence. If the two lines in init of class A swaps, then the result will be expected. I wonder if this is a bug in grammar analysis.

– Geno Chen
Nov 24 '18 at 7:05





Oh you mean this. The results depends on the sequence. If the two lines in init of class A swaps, then the result will be expected. I wonder if this is a bug in grammar analysis.

– Geno Chen
Nov 24 '18 at 7:05













Is there any particular reason why you initialize all properties in init block instead of in-place? Though this doesn't affect this question.

– Alexey Romanov
Nov 24 '18 at 9:48





Is there any particular reason why you initialize all properties in init block instead of in-place? Though this doesn't affect this question.

– Alexey Romanov
Nov 24 '18 at 9:48












2 Answers
2






active

oldest

votes


















2














This is already reported 3 years ago, as KT-10455, "Kotlin allows use of class members before initialization, leading to runtime exceptions, including NPEs on non-null types".



For temporarily fix, you can just swap the two lines in init in class A, make sure A.at is defined before used.



class A
{
val b:B
val at:String
init
{
at="A's text"
b=B(this)
}
}





share|improve this answer































    0














    First you need to initialize variable at and then b:



    class A
    {
    val b:B
    val at:String
    init
    {
    at="A's text"
    b=B(this)

    }
    }





    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',
      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%2f53455804%2fkotlin-incorrect-null-check-warning%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









      2














      This is already reported 3 years ago, as KT-10455, "Kotlin allows use of class members before initialization, leading to runtime exceptions, including NPEs on non-null types".



      For temporarily fix, you can just swap the two lines in init in class A, make sure A.at is defined before used.



      class A
      {
      val b:B
      val at:String
      init
      {
      at="A's text"
      b=B(this)
      }
      }





      share|improve this answer




























        2














        This is already reported 3 years ago, as KT-10455, "Kotlin allows use of class members before initialization, leading to runtime exceptions, including NPEs on non-null types".



        For temporarily fix, you can just swap the two lines in init in class A, make sure A.at is defined before used.



        class A
        {
        val b:B
        val at:String
        init
        {
        at="A's text"
        b=B(this)
        }
        }





        share|improve this answer


























          2












          2








          2







          This is already reported 3 years ago, as KT-10455, "Kotlin allows use of class members before initialization, leading to runtime exceptions, including NPEs on non-null types".



          For temporarily fix, you can just swap the two lines in init in class A, make sure A.at is defined before used.



          class A
          {
          val b:B
          val at:String
          init
          {
          at="A's text"
          b=B(this)
          }
          }





          share|improve this answer













          This is already reported 3 years ago, as KT-10455, "Kotlin allows use of class members before initialization, leading to runtime exceptions, including NPEs on non-null types".



          For temporarily fix, you can just swap the two lines in init in class A, make sure A.at is defined before used.



          class A
          {
          val b:B
          val at:String
          init
          {
          at="A's text"
          b=B(this)
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 7:20









          Geno ChenGeno Chen

          2,0176922




          2,0176922

























              0














              First you need to initialize variable at and then b:



              class A
              {
              val b:B
              val at:String
              init
              {
              at="A's text"
              b=B(this)

              }
              }





              share|improve this answer




























                0














                First you need to initialize variable at and then b:



                class A
                {
                val b:B
                val at:String
                init
                {
                at="A's text"
                b=B(this)

                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  First you need to initialize variable at and then b:



                  class A
                  {
                  val b:B
                  val at:String
                  init
                  {
                  at="A's text"
                  b=B(this)

                  }
                  }





                  share|improve this answer













                  First you need to initialize variable at and then b:



                  class A
                  {
                  val b:B
                  val at:String
                  init
                  {
                  at="A's text"
                  b=B(this)

                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 7:15









                  SergeySergey

                  3,83621734




                  3,83621734






























                      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%2f53455804%2fkotlin-incorrect-null-check-warning%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







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen