The issue occurs in my mapper class mapWeatherEntityToWeatherData,where I made weatherEntity an instance of WeatherEntity none of the values are null even from the API:
object WeatherEntityMapper {
fun mapWeatherEntityToWeatherData(weatherEntity: WeatherEntity): WeatherData {
Log.d("entity null", weatherEntity.toString())
val currentWeather = Weather(
sunrise = weatherEntity.sunrise,
sunset = weatherEntity.sunset,
temperature = weatherEntity.temperature,
feelsLike = weatherEntity.feelsLike,
pressure = weatherEntity.pressure,
humidity = weatherEntity.humidity,
visibility = weatherEntity.visibility,
uvi = weatherEntity.uvi,
windSpeed = weatherEntity.windSpeed,
windDegree = weatherEntity.windDegree,
weather = weatherEntity.weather,
)
return WeatherData(
background = getBackgroundFromId(weatherEntity.background),
current = currentWeather,
forecasts = convertJsonToList(weatherEntity.forecasts)?: emptyList()
)
}
fun mapWeatherDataToWeatherEntity(weatherData: WeatherData): WeatherEntity {
val currentWeather = weatherData.current
return WeatherEntity(
background = getIdFromBackground(weatherData.background),
sunrise = currentWeather.sunrise,
sunset = currentWeather.sunset,
temperature = currentWeather.temperature,
feelsLike = currentWeather.feelsLike,
pressure = currentWeather.pressure,
humidity = currentWeather.humidity,
visibility = currentWeather.visibility,
uvi = currentWeather.uvi,
windSpeed = currentWeather.windSpeed,
windDegree = currentWeather.windDegree,
weather = currentWeather.weather,
forecasts = convertListToJson(weatherData.forecasts)
)
}
@DrawableRes
private fun getBackgroundFromId(id: Int): Int {
return when (id) {
0 -> R.drawable.bg_morning
1 -> R.drawable.bg_day
2 -> R.drawable.bg_evening
else -> R.drawable.bg_night
}
}
private fun getIdFromBackground(@DrawableRes id: Int): Int {
return when (id) {
R.drawable.bg_morning -> 0
R.drawable.bg_day -> 1
R.drawable.bg_evening -> 2
else -> 3
}
}
private fun convertListToJson(forecasts: List<DailyForecast>): String {
val jsonList = forecasts.map { forecast ->
forecast.copy(icon = getIdFromIcon(forecast.icon))
}
return Gson().toJson(jsonList)
}
private fun convertJsonToList(forecasts: String): List<DailyForecast> {
val type = object : TypeToken<List<DailyForecast>>() {}.type
val jsonList: List<DailyForecast> = Gson().fromJson(forecasts, type)
return jsonList.map { forecast ->
forecast.copy(icon = getIconFromId(forecast.icon))
}
}
@DrawableRes
private fun getIconFromId(id: Int): Int {
return when (id) {
0 -> R.drawable.ic_clear_day
1 -> R.drawable.ic_cloudy_day
2 -> R.drawable.ic_rain
3 -> R.drawable.ic_thunderstorm
4 -> R.drawable.ic_snow
5 -> R.drawable.ic_fog
else -> R.drawable.ic_tornado
}
}
private fun getIdFromIcon(@DrawableRes id: Int): Int {
return when (id) {
R.drawable.ic_clear_day -> 0
R.drawable.ic_cloudy_day -> 1
R.drawable.ic_rain -> 2
R.drawable.ic_thunderstorm -> 3
R.drawable.ic_snow -> 4
R.drawable.ic_fog -> 5
else -> 6
}
}
}
This is the weather entity class
@Entity(tableName = "weather_data")
data class WeatherEntity(
@PrimaryKey val id:Int = 1,
val background:Int,
val sunrise:String,
val sunset:String,
val temperature:Double,
val feelsLike:Double,
val pressure:Int,
val humidity:Int,
val visibility:Int,
val uvi:Double,
val windSpeed:Double,
val windDegree:Int,
val weather:String,
val forecasts:String
)
I have tried logging but I can pinpoint the exactly variable causing the null pointer exception where the null value originates.
This is the weather response from the API should it be needed
data class WeatherResponse(
val lat: Double,
val lon: Double,
val timezone: String,
@SerializedName("timezone_offset")
val timezoneOffset: Int,
val current: CurrentWeather,
val daily: List<DailyWeather>,
val hourly: List<HourlyWeather>
)
//these are the data contained in the data classes
data class CurrentWeather(
val dt: Long,
val temp: Double,
val sunrise: Long,
val sunset: Long,
@SerializedName("feels_like")
val feelsLike: Double,
val pressure: Int,
val humidity: Int,
val dewPoint: Double,
val uvi: Double,
val clouds: Int,
val visibility: Int,
@SerializedName("wind_speed")
val windSpeed: Double,
@SerializedName("wind_deg")
val windDeg: Int,
@SerializedName("wind_gust")
val windGust: Double,
val weather: List<Weather>,
)
data class DailyWeather(
val dt: Long,
val temp: Temp,
val sunrise: Long,
val sunset: Long,
val moonrise: Long,
val moonSet: Long,
val moonPhase: Double,
val summary: String,
@SerializedName("feels_like")
val feelsLike: FeelsLike,
val pressure: Int,
val humidity: Int,
@SerializedName("dew_point")
val dewPoint: Double,
@SerializedName("wind_speed")
val windSpeed: Double,
@SerializedName("wind_deg")
val windDeg: Int,
@SerializedName("wind_gust")
val windGust: Double,
val clouds: Int,
val pop: Double,
val uvi: Double,
val weather: List<Weather>,
)
data class HourlyWeather(
val dt: Long,
val temp: Temp,
val weather: List<Weather>,
val pressure: Int,
val humidity: Int,
val dewPoint: Double,
@SerializedName("feels_like")
val feelsLike: FeelsLike,
val uvi: Double,
val clouds: Int,
val visibility: Int,
@SerializedName("wind_speed")
val windSpeed: Double,
@SerialName("wind_deg")
val windDeg: Int,
@SerialName("wind_gust")
val windGust: Double,
)
data class Temp(
val day: Double,
val min: Double,
val max: Double,
val night: Double,
val eve: Double,
val morn: Double
)
data class FeelsLike(
val day: Double,
val night: Double,
val eve: Double,
val morn: Double
)
data class Weather(
val id: Int,
val main: String,
val description: String,
val icon: String
)