You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中Retrofit调用StackExchange API空指针问题求助

解决StackOverflow API请求空对象异常问题

嘿,我帮你找到了问题的核心!你遇到的空指针异常,本质是Gson无法正确解析API返回的JSON数据,导致stackOverflowItems为null,进而在遍历列表时触发了Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()'错误。先看一下你的错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.finalhomework, PID: 5005 java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference at com.example.finalhomework.view.SearchActivity$1.onResponse(SearchActivity.java:46) at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:89) at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$3wC8FyV4pyjrzrYL5U0mlYiviZw.run(lambda) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

问题根源:序列化注解不匹配

StackOverflow API返回的JSON数据中,问题列表对应的字段是items,但你的StackOverflowPageResult类里给items字段加的SerializedName注解是"StackOverflowItem"——这完全不匹配!Gson找不到对应的JSON字段,自然无法把数据映射到你的Java对象中,最终items字段始终为null。

看一下你模型类里的错误代码片段:

@SerializedName("StackOverflowItem") // 这里错了!应该是"items"
@Expose 
private List<StackOverflowItem> items = null; 

public List<StackOverflowItem> getStackOverflowItem() { 
    return items; 
} 

修复方案

1. 修正序列化注解和Getter方法

把StackOverflowPageResult类里的SerializedName改成"items",同时把Getter方法名改成更规范的getItems(),避免命名混淆:

package com.example.finalhomework.model; 
import java.io.Serializable; 
import java.util.List; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class StackOverflowPageResult implements Serializable { 
    @SerializedName("items") // 修正为API返回的正确字段名
    @Expose 
    private List<StackOverflowItem> items = null; 

    @SerializedName("has_more") 
    @Expose 
    private Boolean hasMore; 

    @SerializedName("quota_max") 
    @Expose 
    private Integer quotaMax; 

    @SerializedName("quota_remaining") 
    @Expose 
    private Integer quotaRemaining; 

    private final static long serialVersionUID = -263378404000205617L; 

    public List<StackOverflowItem> getItems() { // 修正Getter方法名
        return items; 
    } 

    public void setItems(List<StackOverflowItem> items) { 
        this.items = items; 
    } 

    // 其他Getter/Setter方法保持不变
}

2. 更新SearchActivity中的调用代码

在onResponse方法里,把原来的getStackOverflowItem()改成修正后的getItems(),同时建议增加非空判断,让代码更健壮:

@Override
public void onResponse(Call<StackOverflowPageResult> call, Response<StackOverflowPageResult> response) {
    StackOverflowPageResult stackOverflowPageResult = response.body();
    if(stackOverflowPageResult != null){ // 先判断响应体是否为空
        stackOverflowItems = stackOverflowPageResult.getItems();
        if(stackOverflowItems != null){ // 再判断列表是否为空
            for (StackOverflowItem s : stackOverflowItems) {
                Log.i("Item StackOverflow :", s.getTitle());
            }
        } else {
            Log.w("Data Warning", "Items list is empty or null");
        }
    } else {
        Log.w("Response Error", "Response body is null");
    }
}

额外小建议

  • 打印原始响应:可以在onResponse里添加Log.d("Raw API Response", response.raw().toString()),方便查看API返回的原始数据,快速排查解析问题。
  • 处理API限流:StackOverflow API有请求配额限制,你可以在解析结果时打印quotaRemaining字段,避免触发限流导致的异常。

内容的提问来源于stack exchange,提问作者yohann belhassen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 17:22:47