I retrieve data from my db put it inside a custom class DataClass
and then I format mydata to an ArrayList of Nodes and until here every thing is fine. Then, I would like to make a loop over my data and add the children to each Node. But I'm having a NullPointerException
error ...
So My class looks like :
@Entity
public class DataClass {
@Id
private Integer id;
private String structureDescription;
private String structId;
private String structureTag;
private Integer elementId;
private String elementDescription;
private Integer elementsSuite;
private String elementTag;
private Integer parent;
private Integer elementTypeId;
Then I have a Node class :
public class Node {
private String id = "-1";
private List<Node> children = null;
private String parent = "#";
private String text = "";
public Node(){}
public Node(String id, String parent, String text) {
this.id = id;
this.parent = parent;
this.text = text;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
public void addChild(Node child)
{
children.add(child);
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public boolean equals(Node node)
{
return node.getText().equals(getText());
}
}
So I have a method that format a List of DataClass objects into anArrayList of Nodes :
public ArrayList<Node> formatData(List<DataClass> dbData)
{
ArrayList<Node> result = new ArrayList<>();
for(DataClass dbDataElement : dbData)
{
Node node = new Node();
node.setId(String.valueOf(dbDataElement.getId()) == null ? "" : String.valueOf(dbDataElement.getId()));
node.setParent(String.valueOf(dbDataElement.getParent()));
node.setText((dbDataElement.getElementTag() == null) ? dbDataElement.getStructId() : dbDataElement.getElementTag());
result.add(node);
}
return result;
}
And then I have a method that is supposed to find all the children for each node :
public ArrayList<Node> addNodeChildren(ArrayList<Node> formattedNodes)
{
ArrayList<Node> result = new ArrayList<>();
for(Node parent: formattedNodes)
{
for(Node possibleChild: formattedNodes)
{
if(String.valueOf(parent.getId()).equals(possibleChild.getParent()))
{
parent.addChild(possibleChild);
}
}
}
return result;
}
But when I call return mainService.addNodeChildren(mainService.formatData(dbData));
from the controller i'm having this error :
09-May-2017 11:06:18.498 SEVERE [http-nio-8080-exec-211] null.null An exception was not mapped due to exception mapper failure. The HTTP 500 response will be re turned.
java.lang.NullPointerExceptionat be.groups.observatory.domainmodel.Node.addChild(Node.java:38) at be.groups.observatory.be.groups.observatory.services.MainService.addNodeChildren(MainService.java:37) at be.groups.ui.resource.StructureResource.getAllChildren(StructureResource.java:130)