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

com.mayokunadeniyi.instantweather, PID: 2897java.lang.NullPointerException at com.mayokunadeniyi.instantweather.data.source.local #53

Open
OmarLkhalil opened this issue Jan 22, 2024 · 1 comment

Comments

@OmarLkhalil
Copy link

class WeatherLocalDataSourceImpl
is thrown a null pointer exception

so returning an empty DBWeather if it's null fixing the issue

override suspend fun getWeather(): DBWeather? = withContext(ioDispatcher) {
if (weatherDao.getWeather() == null) {
return@withContext DBWeather(
cityId = 0,
cityName = "None",
wind = Wind(
speed = 0.0,
deg = 0
),
networkWeatherDescription = listOf(),
networkWeatherCondition = NetworkWeatherCondition(
temp = 0.0,
pressure = 0.0,
humidity = 0.0
)
)
} else {
return@withContext weatherDao.getWeather()
}
}

@OmarLkhalil
Copy link
Author

and you may need to update observeviewmodel function in home fragment also to handle if networkWeatherDescription is empty

weather.observe(viewLifecycleOwner) { weather ->
weather?.let {
prefs.saveCityId(it.cityId)

                if (prefs.getSelectedTemperatureUnit() == activity?.resources?.getString(R.string.temp_unit_fahrenheit))
                    it.networkWeatherCondition.temp =
                        convertCelsiusToFahrenheit(it.networkWeatherCondition.temp)

                binding.weather = it
                if (it.networkWeatherDescription.isNotEmpty()) {
                    binding.networkWeatherDescription = it.networkWeatherDescription.first()
                } else {
                    binding.networkWeatherDescription = null
                }
            }
        }

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