标题表明,当将JSON信息解析到同样位于Fragment内的文本视图中时,我在Android Studio中收到一个空指针异常。
错误日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.test.test.Model.OpenWeatherMap.getName()' on a null object reference
at com.test.test.Tab1Fragment$GetWeather.onPostExecute(Tab1Fragment.java:184)
at com.test.test.Tab1Fragment$GetWeather.onPostExecute(Tab1Fragment.java:150)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.access$600(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
所以我收到了Null异常,但是我不认为对象为Null?
发生错误的行:
txtCity.setText(String.format("%s, %s",openWeatherMap.getName(), openWeatherMap.getSys().getCountry()));
txtLastUpdate.setText(String.format("Refreshed: %s", Common.getDateNow()));
txtDescription.setText(String.format("%s", openWeatherMap.getWeather().get(0).getDescription()));
txtHumidity.setText(String.format("%d%%", openWeatherMap.getMain().getHumidity()));
txtTime.setText(String.format("%s/%s", Common.unixTimeStampToDateTime(openWeatherMap.getSys().getSunrise()),Common.unixTimeStampToDateTime(openWeatherMap.getSys().getSunset())));
txtCelcius.setText(String.format("%.2f °C", openWeatherMap.getMain().getTemp()));
Picasso.with(getActivity())
.load(Common.getImage(openWeatherMap.getWeather().get(0).getIcon()))
.into(imageView);
我的模型类文件:
import java.util.List;
public class OpenWeatherMap {
private Coord cood;
private List<Weather> weather;
private String base;
private Main main;
private Wind wind;
private Rain rain;
private Clouds clouds;
private int dt;
private Sys sys;
private int id;
private String name;
private int cod;
public OpenWeatherMap() {
}
public OpenWeatherMap(Coord cood, List<Weather> weatherList, String base, Main main, Wind wind, Rain rain, Clouds clouds, int dt, Sys sys, int id, String name, int cod) {
this.cood = cood;
this.weather = weatherList;
this.base = base;
this.main = main;
this.wind = wind;
this.rain = rain;
this.clouds = clouds;
this.dt = dt;
this.sys = sys;
this.id = id;
this.name = name;
this.cod = cod;
}
public Coord getCood() {
return cood;
}
public void setCood(Coord cood) {
this.cood = cood;
}
public List<Weather> getWeather() {
return weather;
}
public void setWeather(List<Weather> weather) {
this.weather = weather;
}
public String getBase() {
return base;
}
public void setBase(String base) {
this.base = base;
}
public Main getMain() {
return main;
}
public void setMain(Main main) {
this.main = main;
}
public Wind getWind() {
return wind;
}
public void setWind(Wind wind) {
this.wind = wind;
}
public Rain getRain() {
return rain;
}
public void setRain(Rain rain) {
this.rain = rain;
}
public Clouds getClouds() {
return clouds;
}
public void setClouds(Clouds clouds) {
this.clouds = clouds;
}
public int getDt() {
return dt;
}
public void setDt(int dt) {
this.dt = dt;
}
public Sys getSys() {
return sys;
}
public void setSys(Sys sys) {
this.sys = sys;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
所以getName()不应该发回空指针异常,这是正确的吗?错误线下方的线是否也会与错误线一起发生?我似乎无法理解为什么出现此错误?
我认为Model Class文件中的信息应该发送回JSON信息以解析到文本视图。
我正在遵循一个教程来学习将JSON信息解析到应用程序中,并且还尝试将信息发送到Fragment View。我在网上找到的大多数答案和讨论似乎都在讨论上面的内容,没有任何片段,但我相信仍然可以。