Issue at OnResponse callback in MainActivity.kt

170 views Asked by At

Would you help me please? There is an error refer to OnResponse in MainActivity.kt

Can you find where is the issue?

FATAL EXCEPTION: main Process: com.example.movieapp, PID: 22955 java.lang.NullPointerException at com.example.movieapp.MainActivity$getMovieData$1.onResponse(MainActivity.kt:39) at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$retrofit2-DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:89) at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$$ExternalSyntheticLambda0.run(Unknown Source:6) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7872) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

class MainActivity : AppCompatActivity() {

private var _binding : ActivityMainBinding? = null
private val binding get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    _binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(this)

    binding.rvGenresList.layoutManager = layoutManager
    binding.rvGenresList.setHasFixedSize(true)
    getMovieData { movies: List<Movie> ->
        binding.rvGenresList.adapter = MovieAdapter(movies)
    }
}

private fun getMovieData(callback: (List<Movie>) -> Unit){
    val apiService = MovieApiService.getInstance().create(MovieApiInterface::class.java)
    apiService.getMovieList().enqueue(object : Callback<MovieResponse> {
        override fun onResponse(call: Call<MovieResponse>, response: Response<MovieResponse>) {
            return callback(response.body()!!.movies)
        }

        override fun onFailure(call: Call<MovieResponse>, t: Throwable) {
            TODO("Not yet implemented")
        }

    })
}

}

I'm trying to use recyclerview with binding and using API from moviedb.org.

This is my first time to use it. So, is there I need to do to fix this and render the list to screen?

Thank you

0

There are 0 answers