Java 类在类定义中调用自身

Java 类在类定义中调用自身,java,nullpointerexception,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

是的,很抱歉,另一个空指针异常。我肯定错过了一个主要的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类中定义的组件时,您可以清楚地看到该错误是由addDownStreamConn引起的:

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)

您对下游组件的声明不包括定义。这里有一个空引用,您试图在addDownstreamConn方法中使用它。调用add方法时,将解析为null.add,这是一个NullPointerException。您需要初始化ArrayList。

下游组件的声明不包含定义。这里有一个空引用,您试图在addDownstreamConn方法中使用它。调用add方法时,将解析为null.add,这是一个NullPointerException。您需要初始化ArrayList。

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

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

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

 private ArrayList<Component> downstreamComponents = new ArrayList<>();
您是否已初始化下游组件? 请确保你有

private ArrayList downstreamComponents=新ArrayList

最好是将其分配给接口:

private List downstreamComponents=新的ArrayList

是否已初始化下游组件? 请确保你有

private ArrayList downstreamComponents=新ArrayList

最好是将其分配给接口:


private List downstreamComponents=新的ArrayList

您从不在组件类中初始化ArrayList,这就是为什么当您调用它们时会导致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


您从不在组件类中初始化ArrayList,这就是为什么在调用它们时会导致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


你没有初始化下游组件。如果你没有初始化上游组件,同样的情况也会发生。你没有初始化下游组件。如果你没有初始化上游组件,同样的情况也会发生。没问题,当我们缺少咖啡时会发生。没问题,当我们缺少咖啡时会发生