在 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)

这是我的 activity

的相关内容
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)

}

我的模型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)
    }
}

我认为我的 class 有点不对劲,但即使在浏览了一堆教程之后,我仍无法确定它。任何帮助将不胜感激。

感谢您的宝贵时间。

我假设您想获得一个法术列表。这种情况下你的请求url是错误的,应该是:

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

您的模型 类 应该如下所示:

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,
)