类在类定义中调用自身

时间:2014-04-13 21:45:54

标签: java nullpointerexception

是的,我很抱歉另一个空指针异常。我必须缺少一个主要的Java约定,因为我无法理解为什么这不起作用。我正在删除大部分课程,因为它与问题无关,但我们走了:

public class xCirc 
{
public xCirc()
{
    Circuit exCircuit = new Circuit();

    /* Define/List out the components first.
     * define the String name of its parent by
     * component name */
    Component load = new Component("Lightbulb", "CC");
    Component cc = new Component("CC", "PV array");
    Component battery = new Component("Battery", "CC");
    Component pvArray = new Component("PV array", null);

    // Define the components location in the chain.
    // This isn't recursive so don't worry about the
    // objects not having a proper line.
    pvArray.addDownstreamConn(cc); //add charge controller to the PV array
    cc.addDownstreamConn(battery); //add battery to the charge controller
    cc.addDownstreamConn(load); //add load to the charge controller
    /*
    // Now call the logic that sorts these components into
    // their proper positions in the chain
    exCircuit.addComponent(pvArray);
    exCircuit.addComponent(cc);
    exCircuit.addComponent(battery);
    exCircuit.addComponent(load);

    //Now cleanup to clear memory footprint
    pvArray.getDownstreamConns().clear();
    battery.getDownstreamConns().clear();
    cc.getDownstreamConns().clear(); */
}

}

这是导致错误的类和导致错误的函数:

/**
* This is generic component that represents an object
* as part of a whole circuit.
* @author Brant Unger
* @version 0.1
*/
public class Component
{
private String name; //the string name of the component
private String parentName; //The string name of the component's parent
private float voltageIn; //the voltage coming into the component
private float voltageOut; //the voltage coming out of the component
private float requiredVoltage; //the voltage required to power this component
private ArrayList<Component> downstreamComponents; //a downstream component sending voltage to this component
private ArrayList<Component> upstreamComponents; //an upstream component that this component sends voltage to

/**
 * Default constructor
 */
public Component()
{
    this.name = "unknown";
    this.voltageIn = 0.0f;
    this.voltageOut = 0.0f;
}

/**
 * Constructor to set name on creation
 * @param name String The name of the component
 */
public Component(String name)
{
    this.name = name;
}

/**
 * Constructor to set the name and parent upon creation
 * @param name The name of the component
 * @param parentName The name of the component's parent
 */
public Component(String name, String parentName)
{
    this.name = name;
    this.parentName = parentName;
}

/**
 * Constructor to set the name, voltage coming in, and voltage going
 * out of the component.
 * @param name String The name of the component
 * @param voltageIn float The decimal value of voltage of coming in
 * @param voltageOut float The decimal value of the voltage going out
 */
public Component(String name, float voltageIn, float voltageOut)
{
    this.name = name;
    this.voltageIn = voltageIn;
    this.voltageOut = voltageOut;
}

/**
 * Add a connection downstream to this component
 * @param component Component The object to add into the downstream list
 */
public void addDownstreamConn(Component comp)
{
    Component c = new Component();
    downstreamComponents.add(c);
}

错误,你可以清楚地看到addDownStreamConn()在尝试指向xCirc类中定义的组件时导致错误:

Exception in thread "main" java.lang.NullPointerException
at Component.addDownstreamConn(Component.java:72)
at xCirc.<init>(xCirc.java:25)
at main.main(main.java:7)

4 个答案:

答案 0 :(得分:2)

您对downstreamComponents的声明不包含定义。您有一个null引用,您尝试在addDownstreamConn方法中使用它。当您调用add方法时,此解析为null.add(),即NullPointerException。您需要初始化ArrayList

答案 1 :(得分:2)

在初始化之前,您正在使用实例字段downstreamComponents。您可以避免在每个构造函数上初始化它,执行

 private ArrayList<Component> downstreamComponents = new ArrayList<>();

您必须对upstreamComponents执行相同的操作。另外,您应该"program to interface"

 private List<Component> downstreamComponents = new ArrayList<>();
 private List<Component> upstreamComponents = new ArrayList<>();

答案 2 :(得分:2)

您是否已初始化downstreamComponents? 请确保你有

private ArrayList<Component> downstreamComponents = new ArrayList<Component>();

更好的是将其分配给界面:

private List<Component> downstreamComponents = new ArrayList<Component>();

答案 3 :(得分:1)

您永远不会在Component课程中初始化您的ArrayLists,这就是为什么当您调用它们时会产生NullPointerException

替换:

private ArrayList<Component> downstreamComponents; //a downstream component sending voltage to this component
private ArrayList<Component> upstreamComponents; //an upstream component that this component sends voltage to

通过

private ArrayList<Component> downstreamComponents = new ArrayList<Component>(); //a downstream component sending voltage to this component
private ArrayList<Component> upstreamComponents = new ArrayList<Component>(); //an upstream component that this component sends voltage to