fix NPE bug

This commit is contained in:
Looly
2021-06-28 23:22:20 +08:00
parent ffed1d32fd
commit 1472cdc440
4 changed files with 11 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
package cn.hutool.core.lang.tree;
import cn.hutool.core.comparator.CompareUtil;
import java.io.Serializable;
/**
@@ -74,11 +76,11 @@ public interface Node<T> extends Comparable<Node<T>>, Serializable {
@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@Override
default int compareTo(Node node) {
final Comparable weight = this.getWeight();
if (null != weight) {
final Comparable weightOther = node.getWeight();
return weight.compareTo(weightOther);
if(null == node){
return 1;
}
return 0;
final Comparable weight = this.getWeight();
final Comparable weightOther = node.getWeight();
return CompareUtil.compare(weight, weightOther);
}
}

View File

@@ -12,5 +12,4 @@ public class TreeBuilderTest {
of.build();
of.append(new ArrayList<>());
}
}

View File

@@ -28,7 +28,7 @@ public class TreeTest {
@Test
public void sampleTree() {
public void sampleTreeTest() {
List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
for (Tree<String> tree : treeList) {
Assert.assertNotNull(tree);
@@ -43,7 +43,7 @@ public class TreeTest {
}
@Test
public void tree() {
public void treeTest() {
//配置
TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
@@ -66,5 +66,4 @@ public class TreeTest {
Assert.assertEquals(treeNodes.size(), 2);
}
}