UninitializedPropertyAccessException: lateinit property has not been initialized

谁说我不能喝 提交于 2020-05-15 02:40:10

问题


I have a class which i inject into a ViewModel + ViewModel factory, when initialise the view model in the onCreate method in activity, its says the value being passed through is not initialised.

Below is my code

I am quite new to Kotlin so have tried debugging but am stuck on this issue.

Here is MainActivity code:

class MainActivity: AppCompatActivity(), RepoSelectedListener {


    @Inject
    lateinit var viewModel: MainActivityListViewModel

    lateinit var lifecycleOwner: LifecycleOwner
    lateinit var repoSelectedListener: RepoSelectedListener

    @Inject
    lateinit var repository: RepoRepository


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

        viewModel = ViewModelProviders.of(this, ViewModelFactory(repository)).get(MainActivityListViewModel::class.java)

        repoRecyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = RepoListAdapter(viewModel, lifecycleOwner, repoSelectedListener)
        }


**My ViewModel:**



   class MainActivityListViewModel @Inject constructor(val 
    repoRepository: RepoRepository): BaseViewModel() {

    //private lateinit var repoRepository: RepoRepository
    private var disposable: CompositeDisposable? = null

    private val repos = MutableLiveData<List<Repo>>()
    private val repoLoadError = MutableLiveData<Boolean>()
    private val loading = MutableLiveData<Boolean>()


     init {
        disposable = CompositeDisposable()
        fetchRepos()
      }

     fun getRepos(): LiveData<List<Repo>> {
        return repos
     }
    }

my ViewModelFactory:

   class ViewModelFactory @Inject constructor(private val 
   repoRepository: RepoRepository): ViewModelProvider.Factory{


    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        if 
   (modelClass.isAssignableFrom(MainActivityListViewModel::class.java)) 
   {
            @Suppress("UNCHECKED_CAST")
            return MainActivityListViewModel(this.repoRepository) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")

    }


   }

my Class Repo:

  class RepoRepository @Inject constructor(private val githubRepos: 
    GithubRepos){


    private lateinit var repoService: GithubRepos


    fun getRepositories(): Single<List<Repo>> {
        return repoService.getRepos()
    }

    fun getSingleRepo(owner: String, name: String): Single<Repo> {
        return repoService.getSingleRepo(owner, name)
    }
   }

This is error i receive:

   Unable to start activity ComponentInfo{com.carllewis14.repos/com.carllewis14.repos.ui.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property 
  repository has not been initialized

回答1:


have a look at my answer here (maybe it helps ) Nullable var with `?` vs. lateinit var

Essentially, you are never initializing your repository: RepoRepository.

From the code you have written, you won't need an instance of your repository in your activity as well, it should just be created in the constructor of your ViewModel (which has injection)

You are also going to have a similar issue with private lateinit var repoService: GithubRepos; if it's in the constructor of your object, you don't have to declare it again.




回答2:


This is because you are trying to use the repository before initializing it with an instance without checking its initialization.

When using lateinit nullity cannot be used in that variable.

lateinit var repository: RepoRepository

Then, before using any method of the object, check that it is initialized:

if (::repository.isInitialized) {}

GL




回答3:


it seems the question code is shadowing the injection FYI you can also initialize by lazy :

To create an object that will be initialized at the first access to it, we can use the lazy method:

val lazyValue: ClassWithHeavyInitialization by lazy {
        numberOfInitializations.incrementAndGet()
        ClassWithHeavyInitialization()
    }

https://www.baeldung.com/kotlin-lazy-initialization also see kotlin docs



来源:https://stackoverflow.com/questions/57182703/uninitializedpropertyaccessexception-lateinit-property-has-not-been-initialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!