У меня есть класс Kotlin с некоторыми инициализациями stati c через объект-компаньон. Но я получаю ExceptionInInitializerError, когда пытаюсь запустить свое приложение.
Вот трассировка стека:
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)
.
.
.
И мои два класса:
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
}
}
Страна:
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()
}
}
Я что-то инициализирую в неправильном порядке?
Заранее спасибо.