Yes I am sorry another null pointer exception. I must be missing a major Java convention because I cannot understand why this does not work. I am removing the bulk of the classes because it just isn't pertinent to the problem but here we go:
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(); */
}
}
Here's the class causing the error and the function that causes the error:
/**
* 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);
}
And the error where you can clearly see that addDownStreamConn() is causing the error when it tries to point to a component defined in the xCirc class:
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)