gpt4 book ai didi

java - 无法将子节点添加到树节点

转载 作者:行者123 更新时间:2023-12-02 13:13:08 25 4
gpt4 key购买 nike

我从数据库中检索数据,将其放入自定义类 DataClass 中,然后将 mydata 格式化为节点的 ArrayList,直到这里一切都很好。然后,我想对我的数据进行循环并将子节点添加到每个节点。但我遇到了 NullPointerException 错误...

所以我的类(class)看起来像:

@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;

然后我有一个 Node 类:

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());
}
}

所以我有一个将 DataClass 对象列表格式化为节点数组列表的方法:

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;
}

然后我有一个方法应该找到每个节点的所有子节点:

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;
}

但是当我从 Controller 调用 return mainService.addNodeChildren(mainService.formatData(dbData)); 时,我遇到了此错误:

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)

最佳答案

您从未启动过您的子实例,

有很多修复,其中之一是您可以将方法 addChild 更改为:

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

children.add(child);
}

关于java - 无法将子节点添加到树节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43865622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com