Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postValue null #8

Closed
soheilgk opened this issue Oct 8, 2020 · 1 comment
Closed

postValue null #8

soheilgk opened this issue Oct 8, 2020 · 1 comment

Comments

@soheilgk
Copy link

soheilgk commented Oct 8, 2020

I followed your instructure but I get this error
Attempt to invoke virtual method 'void androidx.lifecycle.MutableLiveData.postValue(java.lang.Object)' on a null object reference
I checked everything but I couldn't find the problem

my code
`

class NewsViewModel(
private val newsRepository: NewsRepository
) : ViewModel() {

val breakingNews: MutableLiveData<Resource<NewsResponse>> = MutableLiveData()
private var breakingNewsPage = 1

init {
    getBreakingNews("us")
}

private fun getBreakingNews(countryCode: String) = viewModelScope.launch {
    breakingNews.postValue(Resource.Loading())
    val response = newsRepository.getBreakingNews(countryCode, breakingNewsPage)
    breakingNews.postValue(handleBreakingNewsResponse(response))
}

private fun handleBreakingNewsResponse(response: Response<NewsResponse>) : Resource<NewsResponse> {
    if(response.isSuccessful) {
        response.body()?.let { resultResponse ->
            return Resource.Success(resultResponse)
        }
    }
    return Resource.Error(response.message())
}

}

`

@soheilgk
Copy link
Author

soheilgk commented Oct 8, 2020

it worked :|

@soheilgk soheilgk closed this as completed Oct 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant