我正在尝试将对象添加到列表时为什么会收到 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)