Ooguro
Ooguro

Reputation: 121

How to fix MainActivity? kotlin.UninitializedPropertyAccessException: lateinit property compositeDiposable has not been initialized

The error I'm getting is " java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ginem.rxjavaandretrofit2demo/com.ginem.rxjavaandretrofit2demo.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property compositeDiposable has not been initialized " This happened as a finished the application and attempted to launch in in my android virtual machine.

I'm creating an andorid app that pulls data from a fake REST Api and displays the data a cardview/recyclerview format using RxJava and Retrofit2.

So far I've attempted to change the dependency versions to a lower version and still no go. I've also added all kotlin plugins listed:

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

package com.ginem.rxjavaandretrofit2demo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.ginem.rxjavaandretrofit2demo.Adapter.PostAdapter
import com.ginem.rxjavaandretrofit2demo.Model.Post
import com.ginem.rxjavaandretrofit2demo.Retrofit.IMyAPI
import com.ginem.rxjavaandretrofit2demo.Retrofit.RetrofitClient
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

  internal lateinit var jsonApi:IMyAPI
  internal lateinit var compositeDiposable: CompositeDisposable

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    // init API
    val retrofit = RetrofitClient.instance
    jsonApi = retrofit.create(IMyAPI::class.java)

    //View
    recycler_posts.setHasFixedSize(true)
    recycler_posts.layoutManager=LinearLayoutManager(this)
    fetchData()
}

private fun fetchData() {
    compositeDiposable.add(jsonApi.posts
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe{posts->displayData(posts)})
}

private fun displayData(posts: List<Post>?) {

    val adapter = PostAdapter(this, posts!!)
    recycler_posts.adapter = adapter
 }

}

Model:App(build.gradle)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
   compileSdkVersion 28
   defaultConfig {
    applicationId "com.ginem.rxjavaandretrofit2demo"
    minSdkVersion 15
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
     "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android- 
        optimize.txt'), 'proguard-rules.pro'
    }
   }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib- 
    jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

//Retrofit2
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

//RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.7'


//Support
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'

}

IMyAPI interface

package com.ginem.rxjavaandretrofit2demo.Retrofit

import com.ginem.rxjavaandretrofit2demo.Model.Post 
import io.reactivex.Observable
import retrofit2.http.GET

interface IMyAPI {

   @get: GET("posts")
   val posts: Observable<List<Post>>
}

RetrofitClient class

package com.ginem.rxjavaandretrofit2demo.Retrofit

import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {

  private var ourInstance: Retrofit? = null

  val instance:Retrofit
  get() {
      if(ourInstance == null){
          ourInstance = Retrofit.Builder()
            .baseUrl("https://jsonplaceholder.typicode.com")
            .addConverterFactory(GsonConverterFactory.create())

 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
             .build()
    }
    return ourInstance!!
 }
}

Upvotes: 0

Views: 1673

Answers (2)

Ooguro
Ooguro

Reputation: 121

As you said Dean, I have set to null. Fixing the issue I simple added

internal  var compositeDisposable: CompositeDisposable = CompositeDisposable();

and it ran perfectly fine.

Upvotes: 0

Dean
Dean

Reputation: 73

The issue is exactly what the error message says. I don't see anywhere where you actually set compositeDisposable before you call add() on it. At that point it's still null. You need to initialise it with compositeDisposable =..., change to to be an input parameter, use a dependency injection framework.

Upvotes: 1

Related Questions