簡體   English   中英

在 Kotlin NullPointerException 初學者中用 GSON 和 Volley 解析 JSON

[英]Parsing JSON with GSON and Volley in Kotlin NullPointerException beginner

我正在嘗試在 GSON 和 Kotlin 中的 Volley 的幫助下為學校項目解析 JSON。 這是我第一次使用這些工具,需要一些幫助。 我收到以下錯誤

2022-04-17 20:57:13.967 24411-24411/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 24411
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter spellList
    at com.example.myapplication.SpellAdapter.<init>(SpellAdapter.kt)
    at com.example.myapplication.RuleActivity.getDataSpells$lambda-1(RuleActivity.kt:84)
    at com.example.myapplication.RuleActivity.$r8$lambda$OVOQfC5FGUmxexEtOchBO7ewf4I(RuleActivity.kt)

這是我活動中的相關部分

fun getDataSpells(){
    val rvSpellData = findViewById<RecyclerView>(R.id.rvSpellData)
    val urlSpells = "https://www.dnd5eapi.co/api/spells/fireball"
    val queue = Volley.newRequestQueue(this)

    val request = StringRequest(Request.Method.GET,urlSpells, { response ->
      
        val data = response.toString()
        Log.d("Logs", data.toString())

        val SpellReportModel = Gson().fromJson(data, SpellReportModel::class.java)
        Log.d("Logs", SpellReportModel.toString())


        rvSpellData.adapter = SpellAdapter(SpellReportModel.SpellReportList)
        rvSpellData.layoutManager = LinearLayoutManager(this)

    }, {
        Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show()
    })
    queue.add(request)

}

我的 model class

data class SpellReportModel(
   var SpellReportList:  ArrayList<SpellReport>
)
data class SpellReport(

   @SerializedName("_id")
   var id: String,
   @SerializedName("index")
   var index: String,
   @SerializedName("name")
   var name: String,
)

還有我的適配器

class SpellAdapter(private val spellList: List<SpellReport>): RecyclerView.Adapter<SpellAdapter.SpellAdapterViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SpellAdapterViewHolder {
            val itemView = LayoutInflater.from(parent.context).inflate(R.layout.activity_rule,
            parent, false)

            return SpellAdapterViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: SpellAdapterViewHolder, position: Int) {
            val currentSpell = spellList[position]

            holder.spellName.text = currentSpell.name
            //holder.spellDesc.text = currentSpell.desc
            //holder.spellRange.text = currentSpell.range
    }

    override fun getItemCount() = spellList.size

    class SpellAdapterViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
            val spellName: TextView = itemView.findViewById(R.id.tv_SpellName)
            val spellDesc: TextView = itemView.findViewById(R.id.tv_SpellDesc)
            val spellRange: TextView = itemView.findViewById(R.id.tv_SpellRange)
    }
}

我認為我的課程有些不對勁,但即使在費力地學習了一堆教程之后,我仍無法確定它。 任何幫助將不勝感激。

感謝您的時間。

我假設您想獲得一份法術列表。 這種情況下你的請求url是錯誤的,應該是:

val urlSpells = "https://www.dnd5eapi.co/api/spells"

你的 model 類應該如下所示:

data class SpellReportModel(
    @SerializedName("count") var count: Int? = null,
    @SerializedName("results") var spellReportList: ArrayList<SpellReport> = arrayListOf()
)
data class SpellReport(
   @SerializedName("url") var url: String? = null,
   @SerializedName("index") var index: String? = null,
   @SerializedName("name") var name: String? = null,
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM