我不确定执行 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)