This is the java.lang.NullPointerException: Attempt to invoke virtual method 'by.homemadeapps.weatherapp.DataModel.Entity.Main by.homemadeapps.weatherapp.DataModel.Entity.ResponseData.getMain()' on a null object reference

Like everything on the manual, created the interface

 public interface WeatherAPI { @GET("/data/2.5/weather&appid=" + Constants.HTTP.API_KEY) Call<ResponseData> getWeather(@Query("q") String city); } 

Service:

 public class WeatherService { private WeatherAPI service; public WeatherService() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://api.openweathermap.org") .addConverterFactory(GsonConverterFactory.create()) .build(); service = retrofit.create(WeatherAPI.class); } public void getWeather(String name, Callback callback) throws IOException { service.getWeather(name).enqueue(callback); } } 

mainActivity:

 public class MainActivity extends AppCompatActivity { @BindView(R.id.CITY_NAME_FIND) EditText mEnterCityName; @BindView(R.id.recyclerView) RecyclerView mRecyclerView; @BindView(R.id.CityName) TextView mCityTextView; @BindView(R.id.Speed) TextView mSpeedView; @BindView(R.id.Humidity) TextView mHumidityView; @BindView(R.id.CurrentDate) TextView mCurrentDateView; @BindView(R.id.refresh_button) Button mRefreshButton; private WeatherService weatherService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); weatherService = new WeatherService(); } @OnClick(R.id.refresh_button) void onClick() { Log.d("working", "fine"); try { weatherService.getWeather("Hrodna", new Callback() { @Override public void onResponse(Call call, Response response) { ResponseData responseData = (ResponseData)response.body(); mHumidityView.setText(responseData.getMain().getHumidity()); } @Override public void onFailure(Call call, Throwable t) { } }); } catch (IOException e) { e.printStackTrace(); } } } package by.homemadeapps.weatherapp.DataModel.Entity; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import java.util.ArrayList; import java.util.List; import javax.annotation.Generated; @Generated("org.jsonschema2pojo") public class ResponseData { @SerializedName("coord") @Expose private Coord coord; @SerializedName("weather") @Expose private List<Weather> weather = new ArrayList<Weather>(); @SerializedName("base") @Expose private String base; @SerializedName("main") @Expose private Main main; @SerializedName("wind") @Expose private Wind wind; @SerializedName("rain") @Expose private Rain rain; @SerializedName("clouds") @Expose private Clouds clouds; @SerializedName("dt") @Expose private Integer dt; @SerializedName("sys") @Expose private Sys sys; @SerializedName("id") @Expose private Integer id; @SerializedName("name") @Expose private String name; @SerializedName("cod") @Expose private Integer cod; /** * @return The coord */ public Coord getCoord() { return coord; } /** * @param coord The coord */ public void setCoord(Coord coord) { this.coord = coord; } /** * @return The weather */ public List<Weather> getWeather() { return weather; } /** * @param weather The weather */ public void setWeather(List<Weather> weather) { this.weather = weather; } /** * @return The base */ public String getBase() { return base; } /** * @param base The base */ public void setBase(String base) { this.base = base; } /** * @return The main */ public Main getMain() { return main; } /** * @param main The main */ public void setMain(Main main) { this.main = main; } /** * @return The wind */ public Wind getWind() { return wind; } /** * @param wind The wind */ public void setWind(Wind wind) { this.wind = wind; } /** * @return The rain */ public Rain getRain() { return rain; } /** * @param rain The rain */ public void setRain(Rain rain) { this.rain = rain; } /** * @return The clouds */ public Clouds getClouds() { return clouds; } /** * @param clouds The clouds */ public void setClouds(Clouds clouds) { this.clouds = clouds; } /** * @return The dt */ public Integer getDt() { return dt; } /** * @param dt The dt */ public void setDt(Integer dt) { this.dt = dt; } /** * @return The sys */ public Sys getSys() { return sys; } /** * @param sys The sys */ public void setSys(Sys sys) { this.sys = sys; } /** * @return The id */ public Integer getId() { return id; } /** * @param id The id */ public void setId(Integer id) { this.id = id; } /** * @return The name */ public String getName() { return name; } /** * @param name The name */ public void setName(String name) { this.name = name; } /** * @return The cod */ public Integer getCod() { return cod; } /** * @param cod The cod */ public void setCod(Integer cod) { this.cod = cod; } } 

What have I done wrong?

UPD did wrong

  public interface WeatherAPI { @GET("/data/2.5/weather&appid=" + Constants.HTTP.API_KEY) Call<ResponseData> getWeather(@Query("q") String city); } 

It should be like this:

 @GET("/data/2.5/weather") Call<ResponseData> getWeather(@Query("q") String city, @Query("APPID")String key); 

and in this situation, NullPointerException errors were avoided, but now after receiving a response immediately goes to onFailure() Why, who knows?

  • First of all, check what comes to you in the Response response , is there really ResponseData - Vladyslav Matviienko
  • @metalurgus and how to check, do not tell? I'm stupid ... - psyclr
  • put a breakpoint through the debugger, and look at the value of the variable - Vladyslav Matviienko
  • @metalurgus in response comes null , means an error in the request? - psyclr
  • it's hard for me to say exactly where. I generally do not use Retrofit. Simply by logic, he said, where there may be a final error. Unfortunately, I will not find the cause of this error quickly, and for a long time I have no time - Vladyslav Matviienko

1 answer 1

It turned out that problem number 2 was due to the fact that JSONschemaToPOJO generated a slightly wrong model and where Double came to JSON, I had Integer in my model

Problem number 1 was solved with the help of @metalurgus, who recommended to dig what comes in response