I've got the following ViewModel: ```class Fundame...
# android
k
I've got the following ViewModel:
Copy code
class FundamentalsViewModel: ViewModel() {

    var fundamentalsLiveData = MutableLiveData<WrappedResult<PriceDataResponse>>()
    private val repository = FundamentalsRespository()
    private var job: Job? = null

    fun getData(symbol: String) {
        if(job == null || job?.isActive == false) {
            fundamentalsLiveData.value = WrappedResult.Loading
            job = viewModelScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
                try {
                    val response = repository.getData(symbol)
                    withContext(Dispatchers.Main) {
                        fundamentalsLiveData.value = WrappedResult.Success(response)
                    }
                } catch(e: Exception) {
                    withContext(Dispatchers.Main) {
  						fundamentalsLiveData.value = WrappedResult.Failure(e)
                    }
                }
            }
        }
    }
}
And out in the field I'm getting a crash that says this:
Copy code
Fatal Exception: java.lang.NullPointerException
Parameter specified as non-null is null: method kotlin.j0.d.u.p, parameter symbol
On the line that is:
Copy code
fundamentalsLiveData.value = WrappedResult.Loading
How is it possible that there is ANY NPE here? It makes no sense to me. The WrappedResult is a typical Kotlin sealed class that looks like this:
Copy code
sealed class WrappedResult<out T> {
    data class Success<out T: Any>(val data:T) : WrappedResult<T>()
    data class Failure(val error: Throwable) : WrappedResult<Nothing>()
    data class CallFailure(val error: String) : WrappedResult<Nothing>()
    object Loading : WrappedResult<Nothing>()
}
Any ideas would be appreciated!
👍 1
r
Response is probably null lol
k
The crash is happening on this line: fundamentalsLiveData.value = WrappedResult.Loading, not the line where I get the result.
r
Perhaps add breakpoints and step thru it,
d
Is
var fundamentalsLiveData = MutableLiveData<WrappedResult<PriceDataResponse>>()
liveData being reset to
null
somewhere?