Ken Maffei
09/16/2020, 6:07 AMclass 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:
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:
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:
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!rkeazor
09/16/2020, 7:34 AMKen Maffei
09/16/2020, 1:13 PMrkeazor
09/16/2020, 11:44 PMdroid
09/17/2020, 5:21 PMvar fundamentalsLiveData = MutableLiveData<WrappedResult<PriceDataResponse>>()
liveData being reset to null
somewhere?