-2

I am running a java program through an application called Anylogic. I am creating a library that stores node objects for a water distribution system. I have provided the code for the two classes below. I am testing this code to see if I can access the nodes and initialize them properly. Any pointers on what is wrong with the code will help me out quite a bit.

/**
* NodeObject
*/  
public class NodeObject implements java.io.Serializable {

private String nodeID;
private boolean gate;
private boolean securityCamera;
private boolean isExposed;
private String nodeType;

/**
 * Default constructor
 */
public NodeObject(){
}

public void NodeObject(String nodeID, boolean gate, boolean securityCamera, boolean isExposed, String nodeType){
    this.nodeID = nodeID;
    this.gate = gate;
    this.securityCamera = securityCamera;
    this.isExposed = isExposed;
    this.nodeType = nodeType;
    return;
}

//--------------------------------------------------------------------------------------------------------------

public void alternateConstruct(String ID, boolean gateInfo, boolean securityCameraInfo, boolean exposureInfo, String nodeTypeInfo) {
    this.nodeID = ID;
    this.gate = gateInfo;
    this.securityCamera = securityCameraInfo;
    this.isExposed = exposureInfo;
    this.nodeType = nodeTypeInfo;
    return;
}

public String getNodeID() {
    String returnval = this.nodeID;
    return returnval;
}

public boolean hasGate() {
    boolean returnval = this.gate;
    return returnval;
}

public boolean hasSecurityCamera() {
    boolean returnval = this.securityCamera;
    return returnval;
}

public boolean exposed() {
    boolean returnval = this.isExposed;
    return returnval;
}

public String getNodeType() {
    String returnval = this.nodeType;
    return returnval;
}

@Override
public String toString() {          
    return super.toString();
}

/**
 * This number is here for model snapshot storing purpose<br>
 * It needs to be changed when this class gets changed
 */ 
private static final long serialVersionUID = 1L;

}



/**
* NodeLibrary
*/  
public class NodeLibrary implements java.io.Serializable {

private String libraryName;
public NodeObject [] nodeArray = new NodeObject[10];

/**
 * Default constructor
 */
public NodeLibrary(){
}


//--------------------------------------------------------------------------

public void initiateNodes() {
    for(int i = 0; i < nodeArray.length; i++) {
        nodeArray[i].alternateConstruct("J16", true, true, true, "Junction");
    }
    return;
}

public String getLibraryName() {
    String returnval = this.libraryName;
    return returnval;
}

public String getNodeID2(int indexValue) {
    String returnval = nodeArray[indexValue].getNodeID();
    return returnval;
}

public boolean hasGate2(int indexValue) {
    boolean returnval = nodeArray[indexValue].hasGate();
    return returnval;
}

public boolean hasSecurityCamera2(int indexValue) {
    boolean returnval = nodeArray[indexValue].hasSecurityCamera();
    return returnval;
}

public boolean isExposed2(int indexValue) {
    boolean returnval = nodeArray[indexValue].exposed();
    return returnval;
}

public String getNodeType2(int indexValue) {
    String returnval = nodeArray[indexValue].getNodeType();
    return returnval;
}

@Override
public String toString() {          
    return super.toString();
}

/**
 * This number is here for model snapshot storing purpose<br>
 * It needs to be changed when this class gets changed
 */ 
private static final long serialVersionUID = 1L;

}

This code is executed in the Anylogic application

NodeLibrary Dtown = new NodeLibrary();
Dtown.initiateNodes();

This is the error message that I get

NullPointerException
java.lang.NullPointerException
    at epanet.NodeLibrary.initiateNodes(NodeLibrary.java:72)
    at epanet.WDS.enterState(WDS.java:765)
    at epanet.WDS.executeActionOf(WDS.java:675)
    at com.xj.anylogic.engine.Statechart.start(Unknown Source)
    at epanet.WDS.start(WDS.java:1886)
    at epanet.Main.start(Main.java:2484)
    at com.xj.anylogic.engine.Engine.start(Unknown Source)
    at com.xj.anylogic.engine.ExperimentSimulation.b(Unknown Source)
    at com.xj.anylogic.engine.ExperimentSimulation.run(Unknown Source)
    at epanet.Simulation.executeShapeControlAction(Simulation.java:85)
Mephy
  • 2,978
  • 3
  • 25
  • 31
  • Please include the exact error message and tell us what line in the file the stack trace corresponds to. – childofsoong Jul 01 '15 at 21:36
  • Have you tried to add a breakpoint at line `NodeLibrary.java:72` and verify the values of variables used at that line? – GUL Jul 01 '15 at 21:43
  • Does this refer to Anylogic the modelling framework / application? If so - you might want to add the agent-based-modelling and/or system-dynamics tags to give the best chance of attracting a specialist audience to your question – J Richard Snape Jul 01 '15 at 22:39

3 Answers3

0

I suspect this: you're initializing nodeArray to an array, but that array is an array of references, which you don't seem to be initializing to instances of NodeObject anywhere. yet, in initiateNodes() you're dereferencing elements of the array, which are null.

Roberto Attias
  • 1,883
  • 1
  • 11
  • 21
0

What I understand about java is when you construct an array of custom defined objects, aka Nodeobject, you have to initialize them one by one.

public NodeObject [] nodeArray = new NodeObject[10];

This create an array of size 10, each is an reference pointer that should point to a NodeObject. But currently they are pointing to null.

A practical way is to initialize these instance variables in constructor

public NodeLibrary(){
    for(int i = 0; i < nodeArray.length; i++) {
        nodeArray[i]=new NodeObject();
    }
}

Then when you call them in initializeNodes function, they are not null.

Evilsanta
  • 1,032
  • 11
  • 18
0

The error message:

java.lang.NullPointerException
at epanet.NodeLibrary.initiateNodes(NodeLibrary.java:72)

tells you that you are accessing an Object that was not instatiated before. This happens at line 72 and is within the method initiateNodes.
From your code it can only be the line:

nodeArray[i].alternateConstruct("J16", true, true, true, "Junction");

At this line you are accessing the method alternateConstruct on an Object that you did not intialize before. You have to call the constructor before that. You could do:

nodeArray[i] = new NodeObject();
nodeArray[i].alternateConstruct("J16", true, true, true, "Junction");

Or you could (and this is the better solution) create a constructor that takes the same arguments as the method alternatConstruct and change the line to:

nodeArray[i] = new NodeObject("J16", true, true, true, "Junction");

Such a constructor would look like this:

public NodeObject(String ID, boolean gateInfo, boolean securityCameraInfo, boolean exposureInfo, String nodeTypeInfo){
    this.nodeID = ID;
    this.gate = gateInfo;
    this.securityCamera = securityCameraInfo;
    this.isExposed = exposureInfo;
    this.nodeType = nodeTypeInfo;
}
MalaKa
  • 3,734
  • 2
  • 18
  • 31
  • This solution worked absolutely wonderful for me. It also gave me some insight on how to properly instantiate objects. Thanks for the help!!! – jakeMonroe Jul 02 '15 at 13:17