Questions tagged [kotlin-null-safety]

153 questions
323
votes
7 answers

What is the Kotlin double-bang (!!) operator?

I'm converting Java to Kotlin with Android Studio. I get double bang after the instance variable. What is the double bang and more importantly where is this documented? mMap!!.addMarker(MarkerOptions().position(london).title("Marker in London"))
mbr_at_ml
  • 3,585
  • 2
  • 13
  • 12
158
votes
6 answers

Best way to null check in Kotlin?

Should I use double =, or triple =? if(a === null) { //do something } or if(a == null) { //do something } Similarly for 'not equals': if(a !== null) { //do something } or if(a != null) { //do something }
pdeva
  • 43,605
  • 46
  • 133
  • 171
75
votes
8 answers

How do I run a block of code if a nullable type is null?

In Kotlin, I can run code if an object is not null like this: data?.let { // execute this block if not null } But how can I execute a block of code if the object is null?
Toni Joe
  • 7,715
  • 12
  • 50
  • 69
50
votes
9 answers

Idiomatic way of handling nullable or empty List in Kotlin

Say I have a variable activities of type List?. If the list is not null and not empty, I want to do something, otherwise I want to do something else. I came up with following solution: when { activities != null && !activities.empty ->…
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
43
votes
7 answers

how to use spring annotations like @Autowired or @Value in kotlin for primitive types?

Autowiring a non-primitive with spring annotations like bellow works: @Autowired lateinit var metaDataService: MetaDataService But this doesn't work: @Value("\${cacheTimeSeconds}") lateinit var cacheTimeSeconds: Int Error: lateinit modifier is…
fkurth
  • 868
  • 1
  • 8
  • 11
39
votes
5 answers

How to idiomatically test for non-null, non-empty strings in Kotlin?

I am new to Kotlin, and I am looking for help in rewriting the following code to be more elegant. var s: String? = "abc" if (s != null && s.isNotEmpty()) { // Do something } If I use the following code: if (s?.isNotEmpty()) { The compiler will…
iForests
  • 6,757
  • 10
  • 42
  • 75
31
votes
2 answers

Is `a?.let{} ?: run{}` idiomatic in Kotlin?

I saw the following comment in a S.O. post, and I'm intrigued: why don't you use if for null checks? a?.let{} ?: run{} is only appropriate in rare cases, otherwise it is not idiomatic – voddan May 15 '16 at 7:29 best way to null check in…
Andy Marchewka
  • 412
  • 1
  • 5
  • 8
29
votes
2 answers

How is Dart "sound null-safety" different from Kotlin null safety?

This Dart official video states that Dart's so-called "sound null safety" is better than Kotlin's null safety design, because it can optimise the code based on whether a variable is declared nullable, and other languages (I assume this refers to…
27
votes
3 answers

Only safe or non null assserted calls are allowed on a nullable receiver type of arraylist

Just started using kotlin for android development.My arraylist is declared like this- var day1: ArrayList? = null Now I am trying to access an element by its position val dietPlan= day1[position] but i am getting below…
Android Developer
  • 9,157
  • 18
  • 82
  • 139
25
votes
3 answers

Kotlin - lateinit VS Any? = null

In Kotlin there appears to be two method of declaring a variable inside an object that can be null and instantiated after the object is created. var myObject : Any? = null or var lateinit myObject : Any I am confused about why the lateinit…
fergdev
  • 963
  • 1
  • 13
  • 27
21
votes
6 answers

Cannot set non-nullable LiveData value to null

The error from the title is returned for the following code, which makes no sense private val _error = MutableLiveData() val error: LiveData get() = _error _error.postValue(null) //Error Cannot set non-nullable LiveData value to…
pedja
  • 3,285
  • 5
  • 36
  • 48
19
votes
1 answer

How to set lateinit Kotlin property to null

The below class has a very unique lifecycle, which requires me to temporarily null out lateinit properties class SalesController : BaseController, SalesView { @Inject lateinit var viewBinder: SalesController.ViewBinder @Inject lateinit var…
ZakTaccardi
  • 12,212
  • 15
  • 59
  • 107
18
votes
6 answers

Kotlin call function only if all arguments are not null

Is there a way in kotlin to prevent function call if all (or some) arguments are null? For example Having function: fun test(a: Int, b: Int) { /* function body here */ } I would like to prevent null checks in case when arguments are null. For…
xinaiz
  • 7,744
  • 6
  • 34
  • 78
15
votes
3 answers

Combine null safety and assertNotNull

In test we usually have assertNotNull, but it does not perform smart cast from a nullable type to a non-nullable. I have to write something like this: if (test == null) { Assert.fail("") return } Is it a workaround to perform smart cast…
Boris
  • 4,944
  • 7
  • 36
  • 69
14
votes
3 answers

Android Room returns Null as Non-Null type

I have Dao to return simple object. If object does not exist, Room return null, but Android app have no crashes. Also if I assign that value to non-null variable, no crashes are in app. Dao: @Query("SELECT * FROM users WHERE id LIKE :id LIMIT…
Francis
  • 6,788
  • 5
  • 47
  • 64
1
2 3
10 11