Android question mark after variable [duplicate]












-2
















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)










share|improve this question















marked as duplicate by Jayson Minard kotlin
Users with the  kotlin badge can single-handedly close kotlin questions as duplicates and reopen them as needed.

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
















-2
















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)










share|improve this question















marked as duplicate by Jayson Minard kotlin
Users with the  kotlin badge can single-handedly close kotlin questions as duplicates and reopen them as needed.

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














-2












-2








-2


0







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)










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 20:57







lostScriptie

















asked Nov 23 '18 at 21:31









lostScriptielostScriptie

457




457




marked as duplicate by Jayson Minard kotlin
Users with the  kotlin badge can single-handedly close kotlin questions as duplicates and reopen them as needed.

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 kotlin
Users with the  kotlin badge can single-handedly close kotlin questions as duplicates and reopen them as needed.

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














  • 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












1 Answer
1






active

oldest

votes


















2














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, and null is the
    result

  • If the value is not null, your function call is made as normal


So, in your case:




  • If somevariable.value is null, the add() call is skipped


  • If somevariable.value is not null, the add() call is made on whatever somevariable.value is







share|improve this answer
























  • 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


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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, and null is the
    result

  • If the value is not null, your function call is made as normal


So, in your case:




  • If somevariable.value is null, the add() call is skipped


  • If somevariable.value is not null, the add() call is made on whatever somevariable.value is







share|improve this answer
























  • 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
















2














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, and null is the
    result

  • If the value is not null, your function call is made as normal


So, in your case:




  • If somevariable.value is null, the add() call is skipped


  • If somevariable.value is not null, the add() call is made on whatever somevariable.value is







share|improve this answer
























  • 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














2












2








2







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, and null is the
    result

  • If the value is not null, your function call is made as normal


So, in your case:




  • If somevariable.value is null, the add() call is skipped


  • If somevariable.value is not null, the add() call is made on whatever somevariable.value is







share|improve this answer













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, and null is the
    result

  • If the value is not null, your function call is made as normal


So, in your case:




  • If somevariable.value is null, the add() call is skipped


  • If somevariable.value is not null, the add() call is made on whatever somevariable.value is








share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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





Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga