jsonkotlinarraylistinitializationfragment

Error :- lateinit property itemsList (ArrayList) has not been initialized, HomeFragment, Kotlin


I am trying to create a story Book app with Kotlin but I am beginner and learning programing. and I try to create this app with local Json file (loadJsonFromAssets) with glide, in home fragment, but I got this error "lateinit property itemsList has not been initialized", I don't know how to fix it, please help me to fix it.

Error :- lateinit property itemsList has not been initialized

Home Fragment Code

class HomeFragment : Fragment() {

 
    private lateinit var bookAdapter: BookAdapter
    private lateinit var recyclerView: RecyclerView
    private lateinit var itemsList: ArrayList<BooksData>

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?

    ): View {
        val view = inflater.inflate(R.layout.fragment_home, container, false)

        // Initialize the RecyclerView
        recyclerView = view.findViewById(R.id.bookRecyclerView)
        recyclerView.layoutManager = GridLayoutManager(requireContext(), 2)
        bookAdapter = BookAdapter(this@HomeFragment, itemsList)
        recyclerView.adapter = bookAdapter
        recyclerView.setHasFixedSize(true)
        itemsList = ArrayList<BooksData>()
        loadData()
        return view
    }

    private fun loadData() {
        val jsonString = loadJson("contents.json")
        val items = parseJson(jsonString)
        bookAdapter.setData(items)

    }

    private fun loadJson(contents: String): String {
        val inputStream = requireContext().assets.open(contents)
        val size = inputStream.available()
        val buffer = ByteArray(size)
        inputStream.read(buffer)
        inputStream.close()
        return String(buffer)
    }

    private fun parseJson(jsonString: String): ArrayList<BooksData> {
        val jsonArray = JSONArray(jsonString)
        val items = ArrayList<BooksData>()
        for (i in 0 until jsonArray.length()) {
            val jsonObject = jsonArray.getJSONObject(i)
            val imageUrl = jsonObject.getString("imageUrl")
            val bookName = jsonObject.getString("bookName")
            items.add(BooksData(imageUrl, bookName))

        }; return items
    }
}

data class

data class BooksData(val bookName:String, val imageUrl:String){
}

Adapter class

class BookAdapter(var context: HomeFragment, private var dataList: ArrayList<BooksData>) :
    RecyclerView.Adapter<BookAdapter.BookViewHolder>() {

    inner class BookViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var bookNameTextView: TextView = itemView.findViewById(R.id.bookName)
        var bookImageView: ImageView = itemView.findViewById(R.id.imageUrl)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.book_category_item, parent, false)
        return BookViewHolder(view)
    }

    override fun getItemCount(): Int = dataList.size
    fun setData(data: ArrayList<BooksData>) {
        dataList.clear()
        dataList.addAll(data)
        notifyDataSetChanged()

    }

    override fun onBindViewHolder(holder: BookViewHolder, position: Int) {
        val item = dataList[position]
        holder.bookNameTextView.text = item.bookName
        Glide.with(context).load(item.imageUrl).placeholder(R.drawable.ic_launcher_background)
            .error(R.drawable.vec_quiz).into(holder.bookImageView)
    }
}

Json file

{
"items": \[
{
"bookName": "Basic Grammar",
"imageUrl": "https://drive.google.com/uc?id=1_eVoTO4IeTW1gAbNBb_0-GWASecBNGgS"

    } ,
    {
      "bookName": "Basic Grammar",
      "imageUrl": "https://drive.google.com/uc?id=1_eVoTO4IeTW1gAbNBb_0-GWASecBNGgS"
    
    }

\]
}

Please Help me to fix this error, thanks


Solution

  • In your fragment's onCreateView itemsList is being used before being initialized:

    bookAdapter = BookAdapter(this@HomeFragment, itemsList)
    //....
    itemsList = ArrayList<BooksData>()
    

    You must initialize lateinit property itemsList first by either:

    itemsList = ArrayList<BooksData>()
    bookAdapter = BookAdapter(this@HomeFragment, itemsList)
    
    private var itemsList = ArrayList<BooksData>()