I am trying to get an JSON object with weather information from a link using API http://api.weatherstack.com/current?access_key=111111111111111&query=New%20York&Lang=en
But I'm getting the following error: 2021-02-11 21:30:07.099 28106-28106/com.app.forecastmvvm E/AndroidRuntime: FATAL EXCEPTION: main Process: com.app.forecastmvvm, PID: 28106 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.app.forecastmvvm.data.response.CurrentWeatherEntry.toString()' on a null object reference at com.app.forecastmvvm.ui.weather.current.CurrentWeatherFragment$onActivityCreated$1.invokeSuspend(CurrentWeatherFragment.kt:42)
I know what is the NullPointerException. I think that my link is not building correctly. What's wrong with that?
const val API_KEY = "111111111111111"
//http://api.weatherstack.com/current?access_key=111111111111111&query=New%20York&Lang=en
interface ApixuWeatherApiService {
@GET(value = "current")
fun getCurrentWeather(
@Query(value = "query") location: String,
@Query(value = "lang") languageCode: String = "en"
): Deferred<CurrentWeatherResponse>
companion object {
operator fun invoke(): ApixuWeatherApiService {
val requestInterceptor = Interceptor { chain ->
val url = chain.request()
.url()
.newBuilder()
.addQueryParameter("access_key", API_KEY)
.build()
val request = chain.request()
.newBuilder()
.url(url)
.build()
return@Interceptor chain.proceed(request)
}
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(requestInterceptor)
.build()
return Retrofit.Builder()
.client(okHttpClient)
.baseUrl("https://api.weatherstack.com/")
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApixuWeatherApiService::class.java)
}
}
}