Home > database >  lateinit property mAdapter has not been initialized. How to solve this problem?
lateinit property mAdapter has not been initialized. How to solve this problem?

Time:01-01

MainActivity.kt

class MainActivity : AppCompatActivity(), NewsItemclicked {
    private lateinit var mAdapter: NewsListAdapter


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        fetchData()
        val adapter = NewsListAdapter( this)
        recyclerView.adapter = mAdapter
    }
    private fun fetchData(){
        val url = "https://saurav.tech/NewsAPI/top-headlines/category/sports/in.json"
        val jsonObjectRequest = JsonObjectRequest(
            Request.Method.GET,
            url,
            null,
            Response.Listener {
                val newsJsonArray = it.getJSONArray("articles")
                val newsArray = ArrayList<News>()
                for(i in 0 until newsJsonArray.length()){
                    val newsJsonObject = newsJsonArray.getJSONObject(i)
                    val news = News(
                        newsJsonObject.getString("title"),
                        newsJsonObject.getString("author"),
                        newsJsonObject.getString("url"),
                        newsJsonObject.getString("urlToImage")
                    )
                    newsArray.add(news)
                }
                mAdapter.updateNews(newsArray)
            },
        Response.ErrorListener{

        }
        )
        MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)

        }

    override fun onItemClicked(item: News) {

    }
}

On the above MainActivity.kt of a News app is given.When I try to run the app the app is crashing. It is showing that lateinit property mAdapter has not been initialized. Please help me to figure out the problem.Please try to explain the simplest way as I am a beginner to Android so it is quite difficult to me to understand it quickly.

Error

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.newstoday, PID: 10633 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newstoday/com.example.newstoday.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property mAdapter has not been initialized at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) Caused by: kotlin.UninitializedPropertyAccessException: lateinit property mAdapter has not been initialized

CodePudding user response:

It seems like you're instantiating your adapter and store the reference in a local variable (adapter) instead of the global one (mAdapter) which you specifically created to update its data once you receive the response from the network request.

If you change your code to:

mAdapter = NewsListAdapter(this)

the crash would be solved.

  • Related