mayokunadeniyi / Instant-Weather

An Android weather application implemented using the MVVM pattern, Retrofit2, Dagger Hilt, LiveData, ViewModel, Coroutines, Room, Navigation Components, Data Binding and some other libraries from the Android Jetpack.
MIT License
767 stars 165 forks source link

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

Open OmarLkhalil opened 7 months ago

OmarLkhalil commented 7 months ago

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 commented 7 months ago

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
                }
            }
        }