gpt4 book ai didi

java - 类在类定义中调用自身

转载 作者:行者123 更新时间:2023-11-29 06:34:48 25 4
gpt4 key购买 nike

是的,我很抱歉另一个空指针异常。我一定是错过了一个主要的 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)

最佳答案

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

关于java - 类在类定义中调用自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23048816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com