Android尝试在空对象引用上调用虚方法'...'

时间:2017-01-10 19:36:05

标签: android android-studio retrofit

当我运行应用程序时,它会给我错误,当我刷新应用程序时崩溃

我的代码是

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
    swipeRefreshLayout.setOnRefreshListener(this);

     city = (TextView)findViewById(R.id.city);

    country = (TextView) findViewById(R.id.country);
    update = (TextView) findViewById(R.id.last_update);
   temp = (TextView) findViewById(R.id.temp);
    state = (TextView) findViewById(R.id.state);

    swipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            swipeRefreshLayout.setRefreshing(true);
            fetchWeather();
        }
    });


}
public void fetchWeather(){
    swipeRefreshLayout.setRefreshing(true);
    WeatherAPI.Factory.getInstance().getWeather().enqueue(new Callback<Weather>() {
        @Override
        public void onResponse(Call<Weather> call, Response<Weather> response) {

            city.setText(response
                    .body()
                    .getQuery()
                    .getResults()
                    .getChannel()
                    .getLocation()
                    .getCity());

            country.setText(response
                    .body()
                    .getQuery()
                    .getResults()
                    .getChannel()
                    .getLocation()
                    .getCountry());
            update.setText(response
                    .body()
                    .getQuery()
                    .getResults()
                    .getChannel()
                    .getLastBuildDate());
            temp.setText( response.body().getQuery().getResults().getChannel().getItem().getCondition().getTemp() +"\u2109");
            state.setText(response.body().getQuery().getResults().getChannel().getLocation().getRegion());



            swipeRefreshLayout.setRefreshing(false);

        }

        @Override
        public void onFailure(Call<Weather> call, Throwable t) {
            Log.e("Failed",t.getMessage());
            swipeRefreshLayout.setRefreshing(false);
        }
    });
}


@Override
public void onRefresh() {
    fetchWeather();
}

}

结果的模型类

public class Results {

@SerializedName("channel")
@Expose
private Channel channel;



public void setChannel(Channel channel) {
    this.channel = channel;
}

public Channel getChannel() {
    return channel;
}

}

Channel的模型类

public class Channel {

@SerializedName("units")
@Expose
private Units units;
@SerializedName("title")
@Expose
private String title;
@SerializedName("link")
@Expose
private String link;
@SerializedName("description")
@Expose
private String description;
@SerializedName("language")
@Expose
private String language;
@SerializedName("lastBuildDate")
@Expose
private String lastBuildDate;
@SerializedName("ttl")
@Expose
private String ttl;
@SerializedName("location")
@Expose
private Location location;
@SerializedName("wind")
@Expose
private Wind wind;
@SerializedName("atmosphere")
@Expose
private Atmosphere atmosphere;
@SerializedName("astronomy")
@Expose
private Astronomy astronomy;
@SerializedName("image")
@Expose
private Image image;
@SerializedName("item")
@Expose
private Item item;

public Units getUnits() {
    return units;
}

public void setUnits(Units units) {
    this.units = units;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getLanguage() {
    return language;
}

public void setLanguage(String language) {
    this.language = language;
}

public String getLastBuildDate() {
    return lastBuildDate;
}

public void setLastBuildDate(String lastBuildDate) {
    this.lastBuildDate = lastBuildDate;
}

public String getTtl() {
    return ttl;
}

public void setTtl(String ttl) {
    this.ttl = ttl;
}

public Location getLocation() {
    return location;
}

public void setLocation(Location location) {
    this.location = location;
}

public Wind getWind() {
    return wind;
}

public void setWind(Wind wind) {
    this.wind = wind;
}

public Atmosphere getAtmosphere() {
    return atmosphere;
}

public void setAtmosphere(Atmosphere atmosphere) {
    this.atmosphere = atmosphere;
}

public Astronomy getAstronomy() {
    return astronomy;
}

public void setAstronomy(Astronomy astronomy) {
    this.astronomy = astronomy;
}

public Image getImage() {
    return image;
}

public void setImage(Image image) {
    this.image = image;
}

public Item getItem() {
    return item;
}

public void setItem(Item item) {
    this.item = item;
}

}

清单文件

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

错误应用程序在wifi中运行。当我运行移动互联网时,它不会出错......

FATAL EXCEPTION: main
                                                                      Process: com.example.prem.climate, PID: 11893
                                                                      java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.prem.climate.model.Channel com.example.prem.climate.model.Results.getChannel()' on a null object reference
                                                                          at com.example.prem.climate.MainActivity$2.onResponse(MainActivity.java:68)
                                                                          at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
                                                                          at android.os.Handler.handleCallback(Handler.java:746)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618

1 个答案:

答案 0 :(得分:0)

似乎response.body().getQuery().getResults()返回null,然后您尝试在getChannel()上调用null