1

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)
Brant Unger
  • 403
  • 2
  • 11

4 Answers4

2

Your declaration of downstreamComponents does not include a definition. You have a null reference there, which you attempt to use in the addDownstreamConn method. When you call the add method, this resolves to null.add(), which is a NullPointerException. You need to initialise your ArrayList.

christopher
  • 26,815
  • 5
  • 55
  • 89
2

You are using the instance field downstreamComponents before initializing it. You can avoid initializing it on each constructor, doing

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

You have to do the same for upstreamComponents. Also, you should "program to interface":

 private List<Component> downstreamComponents = new ArrayList<>();
 private List<Component> upstreamComponents = new ArrayList<>();
Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
2

Do you have initialized downstreamComponents ? please make sure that you have

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

and better is to assign it to interface:

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

RMachnik
  • 3,598
  • 1
  • 34
  • 51
1

you never initialize your ArrayLists in Component class, this is why when you call them it results in a NullPointerException.

Replace :

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

by

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
Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59