已更新
我发现使用嵌套类使用Gson库时出现问题。当Gson构建嵌套对象时,将内部类的引用留给外部类this$0
到null
,使得对外部类的任何引用都产生NullPointerException
(在完整示例下面)。这个已知的Gson问题是解决方案吗?
import com.google.gson.Gson;
public class Outer {
protected Inner inner = null;
public Inner getInner() {
return inner;
}
protected void something(){
}
public void function(){
inner.innerFunction();
}
public class Inner {
public String name = null;
protected void innerFunction(){
something();//exception source
}
}
public static void main(String[] args){
String str = "{\"inner\":{\"name\"=\"test\"}}";
Outer outer = (new Gson()).fromJson(str,Outer.class);
System.out.println(outer.getInner().name);
outer.function(); // exception here
}
}