You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Java的CountryModel、数据类及ArrayList转Kotlin后运行时出错求助

Fixing Your Kotlin Runtime Error

Let's walk through the exact issues causing your crash and how to fix them step by step:

1. NullPointerException from Uninitialized ctrlist

The immediate crash occurs because you declared var ctrlist: Countries? = null but never created an instance of Countries before calling ctrlist!!.createSampleData(). Using !! forces a non-null check, and since ctrlist is still null, it throws a runtime NullPointerException.

This is also unnecessary: your original Java code uses a static method for createSampleData(), which doesn't require an instance of Countries. In Kotlin, we replicate static behavior using a companion object.

2. Incorrect Object Creation Syntax

In Kotlin, you don't use the new keyword to create objects. Your Countries.kt code has lines like new CountryModel(...) which is invalid syntax (even if your compiler didn't flag it, it'll cause runtime issues).

3. Optional: Simplify CountryModel with Kotlin Idioms

Your current CountryModel uses an init block to assign values, which is redundant. Kotlin lets you define properties directly in the primary constructor for cleaner, more idiomatic code.


Corrected Code

Countries.kt

import com.appsmata.myapp.models.CountryModel

class Countries {
    companion object {
        // Acts like a static method from Java
        fun createSampleData(): ArrayList<CountryModel> {
            val countries = ArrayList<CountryModel>()
            // Remove "new" when instantiating objects
            countries.add(CountryModel("Afghanistan", "AF", "93"))
            countries.add(CountryModel("Albania", "AL", "355"))
            countries.add(CountryModel("Sudan", "SD", "249"))
            countries.add(CountryModel("Suriname", "SR", "597"))
            return countries
        }
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    // Use lateinit to declare a non-null variable we'll initialize later
    private lateinit var countries: ArrayList<CountryModel>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        // Call the static-like method directly on Countries (no instance needed)
        countries = ArrayList(Countries.createSampleData())
    }
}

CountryModel.kt (Simplified)

import com.appsmata.myapp.core.Searchable

// Define properties directly in the primary constructor to eliminate boilerplate
class CountryModel(
    var country: String,
    var ccode: String,
    var icode: String
) : Searchable {
    override fun getTitle(): String {
        return "$ccode | $country - $icode"
    }

    fun setName(country: String): CountryModel {
        this.country = country
        return this
    }

    fun setShort(countryCode: String): CountryModel {
        this.ccode = countryCode
        return this
    }

    fun setCode(isoCode: String): CountryModel {
        this.icode = isoCode
        return this
    }
}

Key Takeaways

  • Use companion object in Kotlin to replicate Java static methods/fields
  • Avoid using !! on uninitialized variables—it's a frequent source of crashes
  • Kotlin doesn't require the new keyword to create objects
  • Leverage primary constructors to reduce boilerplate in model/data classes

内容的提问来源于stack exchange,提问作者user11450502

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:36:10