I'm trying to parse JSON with the help of GSON and Volley in Kotlin for a school project. It's my first time using these tools and could use a bit of help. I'm getting the following error
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)
Here are the relevant bits from my 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)
}
My 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,
)
And my adapter
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)
}
}
I think there is something off about my classes but even after wading through a bunch of tutorials I haven't been able to nail it down. Any help would be greatly appreciated.
Thank you for your time.
CodePudding user response:
I assume you want to get a list of spells. In this case your request url is wrong, it should be:
val urlSpells = "https://www.dnd5eapi.co/api/spells"
And your model classes should be like the following:
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,
)