-4

我正在尝试将对象添加到列表时为什么会收到 NullPointerException 的一些实验。下面是我的代码和 StackTrace。当我尝试将对象添加到列表时,您知道为什么会收到 NullPointerException 吗?

这是我的测试课。

package com.greathouse.aggregationtest;

import com.greathouse.aggregationtest.types.Composite;
import com.greathouse.aggregationtest.types.InitializedComponent;
import com.greathouse.aggregationtest.types.NullComponent;
import com.greathouse.aggregationtest.types.NullContainingComponent;

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Testing aggregation" );

        InitializedComponent initializedComponent1 = new InitializedComponent();
        initializedComponent1.setInitializedString("Test Initialization 1 Successful");

        InitializedComponent initializedComponent2 = new InitializedComponent();
        initializedComponent2.setInitializedString("Test Initialization 2 Successful");

        NullComponent nullComponent1 = null;
        NullComponent nullComponent2 = null;

        NullContainingComponent nulLContainingComponent1 = new NullContainingComponent();
        nulLContainingComponent1.setIntendedNullString(null);

        NullContainingComponent nullContainingComponent2 = new NullContainingComponent();
        nullContainingComponent2.setIntendedNullString(null);

        System.out.println(initializedComponent1.getInitializedString());
        System.out.println(initializedComponent2.getInitializedString());
        System.out.println(nullComponent1);
        System.out.println(nullComponent2);
        System.out.println(nulLContainingComponent1.getIntendedNullString());
        System.out.println(nullContainingComponent2.getIntendedNullString());

        Composite composite = new Composite();
        composite.addInitializedComponents(initializedComponent1);
        composite.addInitializedComponents(initializedComponent2);
        composite.addNullComponent(nullComponent1);
        composite.addNullComponent(nullComponent2);
        composite.addNullContainingComponent(nulLContainingComponent1);
        composite.addNullContainingComponent(nullContainingComponent2);
    }
}

这是复合对象。组件对象是带有访问器的简单包装器。

package com.greathouse.aggregationtest.types;

import java.util.List;

public class Composite {
    private List<InitializedComponent> initializedComponents;
    private List<NullComponent> nullcomponents;
    private List<NullContainingComponent> nullContainingComponents;
    /**
     * @return the initializedComponents
     */
    public List<InitializedComponent> getInitializedComponents() {
        return initializedComponents;
    }
    /**
     * @return the nullComponents
     */
    public List<NullComponent> getNullcomponents() {
        return nullcomponents;
    }
    /**
     * @return the nullContainingComponents
     */
    public List<NullContainingComponent> getNullContainingComponents() {
        return nullContainingComponents;
    }
    public void addInitializedComponents(InitializedComponent initializedComponent) {
    this.initializedComponents.add(initializedComponent);
    }
    public void addNullComponent(NullComponent nullcomponent) {
    this.nullcomponents.add(nullcomponent);
    }
    public void addNullContainingComponent(NullContainingComponent nullContainingComponent) {
    this.nullContainingComponents.add(nullContainingComponent);
    }
}

控制台输出。

Testing aggregation
Test Initialization 1 Successful
Test Initialization 2 Successful
null
null
null
null
Exception in thread "main" java.lang.NullPointerException
at com.greathouse.aggregationtest.types.Composite.addInitializedComponents(Composite.java:28)
at com.greathouse.aggregationtest.App.main(App.java:41)
4

2 回答 2

2
  private List<InitializedComponent> initializedComponents;
  private List<NullComponent> nullcomponents;
  private List<NullContainingComponent> nullContainingComponents;

此属性未初始化

您创建了一个实例,Composite但从未定义构造函数来初始化这些属性...

您可以在 Composite 中尝试:

public Composite()
{
     this.initializedComponents = new ArrayList<InitializedComponent>();
     this.nullcomponents = new ArrayList<NullComponent>();
     this.nullContainingComponents = new ArrayList<NullContainingComponent>();
}
于 2013-06-14T01:39:55.260 回答
1

InitializedComponent的 'sList未初始化。:)

private List<InitializedComponent> initializedComponents =
                                   new ArrayList<InitializedComponent>();

NullComponent的名单也是NULLList在尝试向它们添加元素之前,您需要实例化所有成员字段。

private List<NullComponent> nullcomponents =
                            new ArrayList<NullComponent>();
private List<NullContainingComponent> nullContainingComponents =
                                      new ArrayList<NullContainingComponent>();
于 2013-06-14T01:41:49.720 回答