1

I'm using Kotlin in my project. I'm getting this error only on the release version. In the debug version, everything works perfectly. It happens on this line:

postList.addAll(response.body()!!.data)

I previously initialized it like this:

val postList: MutableList<Post> = mutableListOf()

Any idea why this is happening?

Here is the full code

    private fun initRecyclerView(){
        adapter = PostsAdapter(context!!, postList)
        val linearLayoutManager = LinearLayoutManager(context)
        rvPosts.layoutManager = linearLayoutManager
        rvPosts.adapter = adapter
    }

    private fun getPosts(){

        swipToRefresh.isRefreshing = true

        disposable.add(
            apiService.getPostsFeed(
                Config.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(object: DisposableSingleObserver<Response<PostData>>() {
                    override fun onSuccess(response: Response<PostData>) {
                        swipToRefresh.isRefreshing = false

                        when {
                            response.isSuccessful -> {
                                postList.clear()
                                if(response.body() != null){
                                    postList.addAll(response.body()!!.data)
                                    adapter.notifyDataSetChanged()
                                    noPostsLayout.visibility = View.GONE
                                }
                            }
                            response.code() == 404 -> {
                                noPostsLayout.visibility = View.VISIBLE
                            }
                            else -> {
                                Toast.makeText(
                                    context,
                                    getString(R.string.error_general_failure),
                                    Toast.LENGTH_LONG
                                ).show()
                                noPostsLayout.visibility = View.GONE
                            }
                        }
                    }

                    override fun onError(e: Throwable) {
                        Timber.d("onError ${e.message}")
                        swipToRefresh.isRefreshing = false
                        if(!Config.isNetworkConnected(context!!)){
                            Toast.makeText(context, getString(R.string.error_internet_connection), Toast.LENGTH_LONG).show()
                        }else{
                            Toast.makeText(context, getString(R.string.error_server_connection), Toast.LENGTH_LONG).show()
                        }
                    }

                })
        )

    }
13
  • 1
    Attach your code Commented Apr 6, 2020 at 4:32
  • 1
    Not the Response itself, but response.body()!!.data is possibly null. The first thing the addAll() method does is call toArray() on the passed Collection, and that would explain the given Exception. Commented Apr 6, 2020 at 4:41
  • 1
    postList.addAll(response.body()?.data?) Commented Apr 6, 2020 at 4:43
  • 1
    @MikeM. Thanks. I got it now. You're totally right. Commented Apr 6, 2020 at 4:44
  • 1
    Your welcome, but the question is why you have problem just in release one. I think your code had problem in debug but you hadn't known it Commented Apr 6, 2020 at 4:48

1 Answer 1

1

Try postList.addAll(response.body()?.data?) or postList.addAll(response.body()!!.data?)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.