封闭类实例的内部类引用为null

时间:2016-04-27 23:07:37

标签: java android nullpointerexception inner-classes gson

我有一个具有内部类和内部实例集合的类:

public class Outer {

    private List<Inner> items = new ArrayList<Inner>(); 

    ... 

    public List<Inner> getItems(){
        return this.items;
    }

    public int getOuterX() {
        ...
    }
    ...

    public class Inner {
        ... 
        public int getInnerX(){     
            return Outer.this.getOuterX(); // this line throws the exception 
        }
    }
}

在其他课程中,我迭代Outer实例的项目,并为每个实例调用getInnerX()

for (Outer.Inner item : outer.getItems()) {
    item.getInnerX();
}

NullPointerException内置getInnerX(),因为Outer.thisnull

我不知道这是怎么发生的。

修改

我正在准备像@Sotirios Delimanolis要求的例子,我无法重现错误。 所以我的问题没有包括在内。

我正在使用retrofit2,而Outer对象是根据服务器的json响应创建的。

Outer的item属性包含GSon转换器的注释:

@SerializedName("items")
@Expose
private List<Inner> items = new ArrayList<Inner>(); 

Outer没有定义构造函数也没有定义Inner。

这是堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String 
com.example.server_comunication.datatypes.OrderResponse$Order.getStatus()' on a null object reference
      at com.example.server_comunication.datatypes.OrderResponse$Order$Supply.forPlacement(OrderResponse.java:408)
      at com.example.server_comunication.datatypes.OrderResponse$Order$Supply.getRemaining(OrderResponse.java:423)
      at com.example.uiHelpers.UIFactoryPanels.drawPanelSuppliesEditor(UIFactoryPanels.java:142)
      at com.example.OrderFragment.onCreateView(OrderFragment.java:85)
      at android.app.Fragment.performCreateView(Fragment.java:2053)
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
      at android.app.BackStackRecord.run(BackStackRecord.java:833)
      at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
      at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:483)
      at android.support.v13.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
      at android.view.View.measure(View.java:17430)
      at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
      at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
      at android.view.View.measure(View.java:17430)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
      at android.view.View.measure(View.java:17430)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
      at android.view.View.measure(View.java:17430)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
      at android.view.View.measure(View.java:17430)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
      at android.view.View.measure(View.java:17430)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
      at android.view.View.measure(View.java:17430)
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
      at android.view.View.measure(View.java:17430)
      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
      at android.view.Choreographer.doCallbacks(Choreographer.java:580)
      at android.view.Choreographer.doFrame(Choreographer.java:550)
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)

1 个答案:

答案 0 :(得分:4)

Retrofit使用Gson和Gson作弊。 (截至撰写本文时)它使用def search if params[:search].present? @posts = Post.search(params[:search]) else @posts = Post.all end end 类通过sun.misc.Unsafe实例化您的类。这完全跳过了任何构造函数。

对于内部类any declared constructor will implicitly declare its first parameter as the enclosing type to represent the enclosing class' instance。 Gson无法创建该封闭类的实例,但即使它确实如此,这也无关紧要。 Gson完全跳过构造函数,将隐式allocateInstance引用保留为Outer.this

最简单的解决方案是改变你的设计,摆脱内部类。或者,您可以注册并使用自定义反序列化器。以下帖子中有一些例子。

相关帖子: