2 回答

TA貢獻1842條經驗 獲得超21個贊
實際上,我更愿意寫評論,但由于我沒有足夠的聲譽來做到這一點,所以我會寫下我的意見作為答案。我希望我能有所幫助。
在我看來,范圍好像有問題。如果您刪除所有“遙不可及”的代碼,getList()它看起來像這樣
public void method1(){
Object1 obj = new Object1(){}
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(getList().get(0).getName()); // This returns null for the getList().
}
似乎沒有什么getList()可以得到的,所以也許這就是你的問題的原因。

TA貢獻1809條經驗 獲得超8個贊
public void method1(){
Object1 obj = new Object1(){
@Override
public void innerMethod(Object response){
setList(response.list);
// Displaying the result of the getter is for sample purposes
System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.
}
};
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(obj.getList().get(0).getName()); // <-- ***********
}
我認為您從錯誤的對象調用 getList() 。如果您沒有指定要從中調用方法的對象,則假定 this.list(),在這種情況下,您將獲得未初始化的數據成員。
添加回答
舉報