Hey guys, I keep on getting `viewModelFactory late...
# android
j
Hey guys, I keep on getting
viewModelFactory lateinit has not been initialized errpr
when injecting a dagger view model factory into a fragment and initialising the viewmodel using viewmodel lazy delegate property, here is a snippet of my code.. Error :
Copy code
Fatal Exception: kotlin.UninitializedPropertyAccessException
lateinit property viewModelFactory has not been initialized
Code:
Copy code
class DeliveriesFragment() : Fragment(){

    @Inject
    lateinit var daggerViewModelFactory: DaggerViewModelFactory
    private val upcomingDeliveriesViewModel : UpcomingDeliveriesViewModel by viewModels { daggerViewModelFactory }

    override fun onAttach(context: Context) {
        SalesApplication.salesComponent().inject(this)
        super.onAttach(context)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        observeDeliveries()
    }

    private fun observeDeliveries(){
        upcomingDeliveriesViewModel.loadUpcomingUserDeliveries("").observe(viewLifecycleOwner, Observer {
            //displayList()
        })
    }
}
s
What is lateinit exactly doing?
k
the error references
viewModelFactory
but that class has
daggerViewModelFactory
is it somewhere else? the code looks correct though.
1
h
Are you declare
DeliveriesFragment
in DI Module?
j
are you compiling with java 1.8?
m
check that you have the DaggerVMFactory in your graph. call this on onCreate
Copy code
SalesApplication.salesComponent().inject(this)
even better consider to support FragmentFactory and move to constructor injection rather than properties injection
901 Views