I am trying to access a string indicating an API key from Resources through my viewModel class mapViewModel
by having an application class named AppUtils
with the method getKey()
which returns the string resource.
*I do not want to pass any context/activity to the viewModel in order to keep the code around the MVVM architecture :
class AppUtils : Application() {
fun getKey() = resources.getString(R.string.google_maps_key)
}
viewModel:
class MapViewModel : ViewModel() {
private val appKey = AppUtils().getKey()
when running the project i get an error saying that the resources object i am referring to is null
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
any suggestions? Thanks !