Android question mark after variable [duplicate]
This question already has an answer here:
In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them
2 answers
I sometimes see statements like somevariable.value?.add()
What purpose does the question mark serve?
(Sorry, at the time of post I had no idea this was Kotlin, I thought it was java)
kotlin
marked as duplicate by Jayson Minard
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 14:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them
2 answers
I sometimes see statements like somevariable.value?.add()
What purpose does the question mark serve?
(Sorry, at the time of post I had no idea this was Kotlin, I thought it was java)
kotlin
marked as duplicate by Jayson Minard
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 14:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
This is not Java code. It's Kotlin.
– forpas
Nov 23 '18 at 21:34
kotlinlang.org/docs/reference/null-safety.html
– Zoe
Nov 24 '18 at 8:36
You said in another comment " I haven't delved into Kotlin yet even though I know I should be so" ... Therefore you asked this question before you took the time to look it up. Please do the opposite and try to resolve things on your own with reasonable effort before asking.
– Jayson Minard
Nov 25 '18 at 14:33
add a comment |
This question already has an answer here:
In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them
2 answers
I sometimes see statements like somevariable.value?.add()
What purpose does the question mark serve?
(Sorry, at the time of post I had no idea this was Kotlin, I thought it was java)
kotlin
This question already has an answer here:
In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them
2 answers
I sometimes see statements like somevariable.value?.add()
What purpose does the question mark serve?
(Sorry, at the time of post I had no idea this was Kotlin, I thought it was java)
This question already has an answer here:
In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them
2 answers
kotlin
kotlin
edited Nov 29 '18 at 20:57
lostScriptie
asked Nov 23 '18 at 21:31
lostScriptielostScriptie
457
457
marked as duplicate by Jayson Minard
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 14:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jayson Minard
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 25 '18 at 14:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
This is not Java code. It's Kotlin.
– forpas
Nov 23 '18 at 21:34
kotlinlang.org/docs/reference/null-safety.html
– Zoe
Nov 24 '18 at 8:36
You said in another comment " I haven't delved into Kotlin yet even though I know I should be so" ... Therefore you asked this question before you took the time to look it up. Please do the opposite and try to resolve things on your own with reasonable effort before asking.
– Jayson Minard
Nov 25 '18 at 14:33
add a comment |
1
This is not Java code. It's Kotlin.
– forpas
Nov 23 '18 at 21:34
kotlinlang.org/docs/reference/null-safety.html
– Zoe
Nov 24 '18 at 8:36
You said in another comment " I haven't delved into Kotlin yet even though I know I should be so" ... Therefore you asked this question before you took the time to look it up. Please do the opposite and try to resolve things on your own with reasonable effort before asking.
– Jayson Minard
Nov 25 '18 at 14:33
1
1
This is not Java code. It's Kotlin.
– forpas
Nov 23 '18 at 21:34
This is not Java code. It's Kotlin.
– forpas
Nov 23 '18 at 21:34
kotlinlang.org/docs/reference/null-safety.html
– Zoe
Nov 24 '18 at 8:36
kotlinlang.org/docs/reference/null-safety.html
– Zoe
Nov 24 '18 at 8:36
You said in another comment " I haven't delved into Kotlin yet even though I know I should be so" ... Therefore you asked this question before you took the time to look it up. Please do the opposite and try to resolve things on your own with reasonable effort before asking.
– Jayson Minard
Nov 25 '18 at 14:33
You said in another comment " I haven't delved into Kotlin yet even though I know I should be so" ... Therefore you asked this question before you took the time to look it up. Please do the opposite and try to resolve things on your own with reasonable effort before asking.
– Jayson Minard
Nov 25 '18 at 14:33
add a comment |
1 Answer
1
active
oldest
votes
Kotlin treats null
as something more than the source of null-pointer exceptions.
In your code snippet, somevariable.value
is of a "nullable type", such as MutableList?
or Axolotl?
. A MutableList
cannot be null
, but a MutableList?
might be null
.
Normally, to call a function on an object, you use a .
.
One option for calling a function on a variable, parameter, or property that is
of a nullable type is to use ?.
. Then, one of two things will happen:
- If the value is
null
, your function call is ignored, andnull
is the
result - If the value is not
null
, your function call is made as normal
So, in your case:
If
somevariable.value
isnull
, theadd()
call is skippedIf
somevariable.value
is notnull
, theadd()
call is made on whateversomevariable.value
is
one possibly should also take!!
for@NonNull
into account.
– Martin Zeitler
Nov 23 '18 at 22:32
there is a book, already: kotlinlang.org/docs/reference/null-safety.html
– Martin Zeitler
Nov 23 '18 at 22:38
Thank you - I haven't delved into Kotlin yet even though I know I should be so this check threw me off. I can clearly see the value of Kotlin.
– lostScriptie
Nov 24 '18 at 17:07
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Kotlin treats null
as something more than the source of null-pointer exceptions.
In your code snippet, somevariable.value
is of a "nullable type", such as MutableList?
or Axolotl?
. A MutableList
cannot be null
, but a MutableList?
might be null
.
Normally, to call a function on an object, you use a .
.
One option for calling a function on a variable, parameter, or property that is
of a nullable type is to use ?.
. Then, one of two things will happen:
- If the value is
null
, your function call is ignored, andnull
is the
result - If the value is not
null
, your function call is made as normal
So, in your case:
If
somevariable.value
isnull
, theadd()
call is skippedIf
somevariable.value
is notnull
, theadd()
call is made on whateversomevariable.value
is
one possibly should also take!!
for@NonNull
into account.
– Martin Zeitler
Nov 23 '18 at 22:32
there is a book, already: kotlinlang.org/docs/reference/null-safety.html
– Martin Zeitler
Nov 23 '18 at 22:38
Thank you - I haven't delved into Kotlin yet even though I know I should be so this check threw me off. I can clearly see the value of Kotlin.
– lostScriptie
Nov 24 '18 at 17:07
add a comment |
Kotlin treats null
as something more than the source of null-pointer exceptions.
In your code snippet, somevariable.value
is of a "nullable type", such as MutableList?
or Axolotl?
. A MutableList
cannot be null
, but a MutableList?
might be null
.
Normally, to call a function on an object, you use a .
.
One option for calling a function on a variable, parameter, or property that is
of a nullable type is to use ?.
. Then, one of two things will happen:
- If the value is
null
, your function call is ignored, andnull
is the
result - If the value is not
null
, your function call is made as normal
So, in your case:
If
somevariable.value
isnull
, theadd()
call is skippedIf
somevariable.value
is notnull
, theadd()
call is made on whateversomevariable.value
is
one possibly should also take!!
for@NonNull
into account.
– Martin Zeitler
Nov 23 '18 at 22:32
there is a book, already: kotlinlang.org/docs/reference/null-safety.html
– Martin Zeitler
Nov 23 '18 at 22:38
Thank you - I haven't delved into Kotlin yet even though I know I should be so this check threw me off. I can clearly see the value of Kotlin.
– lostScriptie
Nov 24 '18 at 17:07
add a comment |
Kotlin treats null
as something more than the source of null-pointer exceptions.
In your code snippet, somevariable.value
is of a "nullable type", such as MutableList?
or Axolotl?
. A MutableList
cannot be null
, but a MutableList?
might be null
.
Normally, to call a function on an object, you use a .
.
One option for calling a function on a variable, parameter, or property that is
of a nullable type is to use ?.
. Then, one of two things will happen:
- If the value is
null
, your function call is ignored, andnull
is the
result - If the value is not
null
, your function call is made as normal
So, in your case:
If
somevariable.value
isnull
, theadd()
call is skippedIf
somevariable.value
is notnull
, theadd()
call is made on whateversomevariable.value
is
Kotlin treats null
as something more than the source of null-pointer exceptions.
In your code snippet, somevariable.value
is of a "nullable type", such as MutableList?
or Axolotl?
. A MutableList
cannot be null
, but a MutableList?
might be null
.
Normally, to call a function on an object, you use a .
.
One option for calling a function on a variable, parameter, or property that is
of a nullable type is to use ?.
. Then, one of two things will happen:
- If the value is
null
, your function call is ignored, andnull
is the
result - If the value is not
null
, your function call is made as normal
So, in your case:
If
somevariable.value
isnull
, theadd()
call is skippedIf
somevariable.value
is notnull
, theadd()
call is made on whateversomevariable.value
is
answered Nov 23 '18 at 21:45
CommonsWareCommonsWare
773k13918891936
773k13918891936
one possibly should also take!!
for@NonNull
into account.
– Martin Zeitler
Nov 23 '18 at 22:32
there is a book, already: kotlinlang.org/docs/reference/null-safety.html
– Martin Zeitler
Nov 23 '18 at 22:38
Thank you - I haven't delved into Kotlin yet even though I know I should be so this check threw me off. I can clearly see the value of Kotlin.
– lostScriptie
Nov 24 '18 at 17:07
add a comment |
one possibly should also take!!
for@NonNull
into account.
– Martin Zeitler
Nov 23 '18 at 22:32
there is a book, already: kotlinlang.org/docs/reference/null-safety.html
– Martin Zeitler
Nov 23 '18 at 22:38
Thank you - I haven't delved into Kotlin yet even though I know I should be so this check threw me off. I can clearly see the value of Kotlin.
– lostScriptie
Nov 24 '18 at 17:07
one possibly should also take
!!
for @NonNull
into account.– Martin Zeitler
Nov 23 '18 at 22:32
one possibly should also take
!!
for @NonNull
into account.– Martin Zeitler
Nov 23 '18 at 22:32
there is a book, already: kotlinlang.org/docs/reference/null-safety.html
– Martin Zeitler
Nov 23 '18 at 22:38
there is a book, already: kotlinlang.org/docs/reference/null-safety.html
– Martin Zeitler
Nov 23 '18 at 22:38
Thank you - I haven't delved into Kotlin yet even though I know I should be so this check threw me off. I can clearly see the value of Kotlin.
– lostScriptie
Nov 24 '18 at 17:07
Thank you - I haven't delved into Kotlin yet even though I know I should be so this check threw me off. I can clearly see the value of Kotlin.
– lostScriptie
Nov 24 '18 at 17:07
add a comment |
1
This is not Java code. It's Kotlin.
– forpas
Nov 23 '18 at 21:34
kotlinlang.org/docs/reference/null-safety.html
– Zoe
Nov 24 '18 at 8:36
You said in another comment " I haven't delved into Kotlin yet even though I know I should be so" ... Therefore you asked this question before you took the time to look it up. Please do the opposite and try to resolve things on your own with reasonable effort before asking.
– Jayson Minard
Nov 25 '18 at 14:33