0

我不确定执行 for 循环时哪个变量为空。是未初始化的条目吗?

MainActivity.java

    static class Gist {
        Map<String, GistFile> files;
    }

    static class GistFile {
        String content;
    }

    Gist gist = gistJsonAdapter.fromJson(responseBody.source());

    for (Map.Entry<String, GistFile> entry : gist.files.entrySet()) {
              System.out.println(entry.getKey());
              System.out.println(entry.getValue().content);
    }

错误信息

java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Set java.util.Map.entrySet()' on a null object reference
        at com.example.soccerapi.MainActivity$1.onResponse(MainActivity.java:76)
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
4

1 回答 1

0

gist.files一片空白。由于它来自responseBody要么确保它在响应中设置,要么在使用它之前检查它是否为空。

于 2020-10-10T06:36:20.837 回答