Kotlin Incorrect null check warning
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
add a comment |
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
a.at
's type isString
, 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
Evena.at
is non-null, the conditiona.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 ininit
ofclass 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 ininit
block instead of in-place? Though this doesn't affect this question.
– Alexey Romanov
Nov 24 '18 at 9:48
add a comment |
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
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
kotlin
edited Nov 26 '18 at 13:33
poocharali
asked Nov 24 '18 at 6:38
poocharalipoocharali
14
14
a.at
's type isString
, 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
Evena.at
is non-null, the conditiona.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 ininit
ofclass 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 ininit
block instead of in-place? Though this doesn't affect this question.
– Alexey Romanov
Nov 24 '18 at 9:48
add a comment |
a.at
's type isString
, 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
Evena.at
is non-null, the conditiona.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 ininit
ofclass 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 ininit
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
add a comment |
2 Answers
2
active
oldest
votes
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)
}
}
add a comment |
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)
}
}
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%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
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)
}
}
add a comment |
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)
}
}
add a comment |
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)
}
}
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)
}
}
answered Nov 24 '18 at 7:20
Geno ChenGeno Chen
2,0176922
2,0176922
add a comment |
add a comment |
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)
}
}
add a comment |
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)
}
}
add a comment |
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)
}
}
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)
}
}
answered Nov 24 '18 at 7:15
SergeySergey
3,83621734
3,83621734
add a comment |
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%2f53455804%2fkotlin-incorrect-null-check-warning%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
a.at
's type isString
, 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 conditiona.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
ofclass 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