0

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)
        }
    }
}
Vlad Starostenko
  • 121
  • 1
  • 10
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Feb 12 '21 at 12:27
  • `Attempt to invoke virtual method 'java.lang.String com.app.forecastmvvm.data.response.CurrentWeatherEntry.toString()' on a null object ` – a_local_nobody Feb 12 '21 at 12:28
  • There can be two possible causes: 1) null response for the api request made due to internet failure or request timeout. 2) Incorrect design of CurrentWeatherEntry data class which will be used by gson for Json serialization. – Amol Desai Feb 12 '21 at 12:38

1 Answers1

0

What the error says is that the toString() method on an object of type CurrentWeatherEntry has been called. Maybe you have a println() or other logging statement somewhere? The error says that wherever you call the print/log of the object CurrentWeatherEntry, that object is null.

Stefan Zhelyazkov
  • 2,599
  • 4
  • 16
  • 41