0

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.NullPointerException

    at 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)
Grechka Vassili
  • 853
  • 4
  • 15
  • 32
  • 1
    Did you debug your code? With an IDE? – mtyurt May 09 '17 at 09:09
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – f1sh May 09 '17 at 09:09
  • @mtyurt I'm working on Intellij that isn't showing any error... – Grechka Vassili May 09 '17 at 09:11
  • 2
    it seems that `children` is never initialised and therefore `null`, ergo NPE when adding a child – Emerson Cod May 09 '17 at 09:12
  • `children` is only initialised in the `Node` class when `setChildren()` is called. Check to see if `children` is `null` in the `addChild()` method. Edit: beaten too it ^^ – d.j.brown May 09 '17 at 09:13

1 Answers1

1

you never initiated your children instance,

There are many fixes one of which is that you can change your method addChild to this:

   public void addChild(Node child)
    {
        if(children==null){
           children=new ArrayList<Node>();
        }

        children.add(child);
    }
nafas
  • 5,283
  • 3
  • 29
  • 57
  • Is it better to change the children property in the node class from `private List children = null;`to `private List children = new ArrayList<>();` ? – Grechka Vassili May 09 '17 at 09:21
  • @GrechkaVassili my personal opinion is to keep children null if class doesn't have children. this way you reduce the number of object created and stored in memory. please bare in mind its just personal preferences , not saying ur solution is wrong – nafas May 09 '17 at 09:24