After kotlin being the first language for android, I have devoted myself into it. With a little progress over the days I have been migrating my existing knowledge to kotlin. Recently, I am learning how to use GSON, Retrofit and kotlin in a dummy project.
Here CurrentWeather is the model which shows data in a view
data class CurrentWeather(
val latitude: Double,
val longitude: Double,
val placeName: String,
val temperature: Float,
val maxTemperature: Float,
val minTemperature: Float,
val windSpeed: Float,
val windDirection: Float,
val weatherType: String,
val weatherDescription: String,
val icon: String,
val timestamp: Instant)
class Current is responsible to parse the JSON to POJO class just like I did in the past, but today it just looks a little different for using kotlin
data class Current(@SerializedName("coord") val location: Location,
@SerializedName("weather") val weather: List<Weather>,
@SerializedName("main") val temperatureAndPressure: TemperatureAndPressure,
@SerializedName("wind") val wind: Wind,
@SerializedName("dt") val timeStamp: Long,
@SerializedName("name") val placeName: String) {
val time: Instant by fastLazy { Instant.ofEpochSecond(timeStamp) }
val currentWeather = CurrentWeather(location.latitude,
location.longitude,
placeName,
temperatureAndPressure.temperature,
temperatureAndPressure.maxTemperature,
temperatureAndPressure.minTemperature,
wind.windSpeed ?: 0f,
wind.windAngle ?: 0f,
weather[0].main,
weather[0].description,
weather[0].icon,
time)
}
Even if I get a successful response from retrofit(I have checked the members variables; such as location: Location, weather: List, temperatureAndPressure: TemperatureAndPressure etc. However, I am getting this error.
2018-11-12 21:04:07.455 9948-9948/bus.green.fivedayweather E/AndroidRuntime: FATAL EXCEPTION: main Process: bus.green.fivedayweather, PID: 9948 java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter p1 at bus.green.fivedayweather.ui.CurrentWeatherFragment$retrieveForecast$1.invoke(Unknown Source:6) at bus.green.fivedayweather.ui.CurrentWeatherFragment$retrieveForecast$1.invoke(CurrentWeatherFragment.kt:20) at bus.green.fivedayweather.net.OpenWeatherMapProvider$RetrofitCallbackWrapper.onResponse(OpenWeatherMapProvider.kt:62)
Am I doing anything wrong in parsing?
retrieveForecastand what calls it? That seems to be what the stacktrace is referring to - Eamon Scullion