Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExceptionInInitializerError in Kotlin static initialization

Tags:

kotlin

I have a Kotlin class with some static initializations through a companion object. But I am getting an ExceptionInInitializerError when I try to run my app.

Here is the stack trace:

 Caused by: java.lang.ExceptionInInitializerError
    at com.myapp.model.location.CountryList.getSelectableCountryList(CountryList.kt:19)
    at com.myapp.ui.util.CountryStateSpinnerSet.<init>(CountryStateSpinnerSet.java:43)
    at java.lang.reflect.Constructor.newInstance0(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
    .
    .
    .
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.get(java.lang.Object)' on a null object reference
    at com.myapp.model.location.Country.<init>(Country.kt:31)
    at com.myapp.model.location.Country.<init>(Country.kt:17)
    at com.myapp.model.location.Country.<clinit>(Country.kt:19)
    at com.myapp.model.location.CountryList.getSelectableCountryList(CountryList.kt:19) 
    at com.myapp.ui.util.CountryStateSpinnerSet.<init>(CountryStateSpinnerSet.java:43) 
    at java.lang.reflect.Constructor.newInstance0(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
    .
    .
    .

And my two classes:

CountryList:

object CountryList {
  val countryList: List<Country>
    get() {
        return listOf(
                Country.UNITED_STATES, Country.CANADA
        )
    }

  @JvmStatic val selectableCountryList: List<Country>
    get() {
        val countryList: MutableList<Country> = mutableListOf(Country.SELECT) <-- the ExceptionInInitializerError occurs on this line
        countryList.addAll(countryList)

        return countryList
    }
}

Country:

data class Country(val name: String, val abbrev: String = ""): Parcelable {
  companion object {
    val SELECT = Country("Select Country")

    val UNITED_STATES = Country("United States", "US")
    val CANADA = Country("Canada", "CA")

    private var countryStateMap: HashMap<Country, StateList> = hashMapOf(
        UNITED_STATES to USStateList(),
        CANADA to CanadaProvinceList()
    )
  }

@IgnoredOnParcel
val stateList: StateList = countryStateMap[this]!!

@IgnoredOnParcel
val selectableStateList: List<State>
    get() = if (null != countryStateMap[this]) {
        countryStateMap[this]!!.selectableStateList
    }
    else {
        throw IllegalStateException("Can't retrieve selectable state list with country: $this")
    }

override fun toString(): String {
    val sb = StringBuilder()
    if (abbrev.isNotEmpty()) {
        sb.append(abbrev)
        sb.append(" - ")
    }
    sb.append(name)
    return sb.toString()
}
}

Am I initializing something in the wrong order?

Thank you in advance.

like image 265
NLam Avatar asked Aug 11 '26 23:08

NLam


1 Answers

I assume Country.SELECT is not yet initialized when accessed. Try to change the order of initialization, by adding by lazy, which tells kotlin to initialize the CountryList later.

object CountryList {
  val countryList: List<Country> by lazy {
     ...

CountryList will be initialized the first time it is actually used. By then Country.SELECT is available.

like image 95
constanze Avatar answered Aug 13 '26 22:08

constanze