Reputation: 1147
I am working through Google's Android developer codelab for the the unscramble app. I am trying to initialize my variable wordsList
to be an empty mutable list of strings with:
private var wordsList: MutableList<String> = mutableListOf()
However, during my debugging process, I see that the value of wordsList
is null
and not an empty list like []
. I tested the code in kotlin playground and it should be []
. I attached a screenshot of what android studio is showing me. What am I doing wrong, why am I not seeing the expected behavior?
Here is the gradle file for my project:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.21"
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
and the gradle file for my app
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.android.unscramble"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding = true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
// LiveData
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
// ViewModel
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
implementation 'androidx.fragment:fragment-ktx:1.2.5'
}
Upvotes: 3
Views: 1378
Reputation: 21
I stumbled into the same problem. Putting the init block after the variable declaration solved the problem for me
private var wordsList: MutableList<String> = mutableListOf()
...
init {
Log.d("GameFragment", "GameViewModel created!")
getNextWord()
}
Upvotes: 0
Reputation: 86
Referring to Kotlin docs:
During an instance initialization, the initializer blocks are executed in the same order as they appear in the class body, interleaved with the property initializers
So, your breakpoint is probably hit from the upper init { }
block when wordsList
is not initialized yet. You may choose to initialize your variable before calling getNextWord()
or just move this init { }
block down so it is executed when all your properties are already initialized.
Upvotes: 7