这段代码一直报空指针,找不到解决方案
//这个是domain里边的package edu.njcc.rj1621.domain;
import java.util.ArrayList;
import java.util.List;
public class ObjectTreeNode {
private Object object;
private List<ObjectTreeNode> children;
public ObjectTreeNode() {
this.children= new ArrayList<ObjectTreeNode>();
}
public ObjectTreeNode(Object object) {
this.object = object;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
public List<ObjectTreeNode> getchildern() {
return children;
}
public void setList(List<ObjectTreeNode> children) {
this.children = children;
}
public void addChild(ObjectTreeNode obj){
this.children.add(obj);
};
}
//这个是Client里边的 可以忽略showBbsTree这个方法 因为还没运行到那 是在list2Tree里边报错的 我会在后边贴运行结果
package edu.njcc.rj1621.client;
import java.util.List;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import edu.njcc.rj1621.domain.Bbs;
import edu.njcc.rj1621.domain.BbsTreeNode;
import edu.njcc.rj1621.domain.ObjectTreeNode;
import edu.njcc.rj1621.service.BbsSvr;
import edu.njcc.rj1621.service.BbsSvrImpl;
public class CopyOfTestBbsQueryList {
public static void main(String[] args) {
BbsSvr bbsSvr = new BbsSvrImpl();
List<Bbs> list = bbsSvr.queryBbsList(1);
ObjectTreeNode ob = new ObjectTreeNode();
Object list2 = list;
for (Bbs bbs : list) {
if (bbs.getBbsPid() == 0) {
ObjectTreeNode child = new ObjectTreeNode(bbs);
list2Tree(child, list2);
ob.getchildern().add(child);
}
}
List<ObjectTreeNode> children = ob.getchildern();
// for (ObjectTreeNode child : children) {
// showBbsTree(child,0);
// }
}
private static void showBbsTree(ObjectTreeNode root,int level) {
for (int i = 0; i < level; i++) {
System.out.print("\t");
}
Object object = root.getObject();
Method method;
try {
method = object.getClass().getMethod("getContent", null);
Object invoke = method.invoke(object, null);
System.out.println(invoke);
List<ObjectTreeNode> list = root.getchildern();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// List<BbsTreeNode> children = root.getChildren();
// for (BbsTreeNode child : children) {
// showBbsTree(child,level+1);
// }
}
private static void list2Tree(ObjectTreeNode root, Object list) {
List list1=(List) list;
for (Object object : list1) {
try {
try {
Object object2 = root.getObject();
Method m2 = object2.getClass().getMethod("getBbsId", null);
Method m1 = object.getClass().getMethod("getBbsPid", null);
Object invoke = m1.invoke(object, null);
Object invoke2 = m2.invoke(object2, null);
String string = invoke.toString();
String string2 = invoke2.toString();
int i = Integer.parseInt(string);
int j = Integer.parseInt(string2);
// System.out.println(string);
// System.out.println(string2);
if (i==j) {
System.out.println("你进来了");
ObjectTreeNode obj = new ObjectTreeNode(object);
list2Tree(obj,list);
System.out.println("你到第二步了");
root.addChild(obj);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
}
}
//这是运行结果
你进来了
你进来了
你进来了
你到第二步了
Exception in thread "main" java.lang.NullPointerException
at edu.njcc.rj1621.domain.ObjectTreeNode.addChild(ObjectTreeNode.java:34)
at edu.njcc.rj1621.client.CopyOfTestBbsQueryList.list2Tree(CopyOfTestBbsQueryList.java:96)
at edu.njcc.rj1621.client.CopyOfTestBbsQueryList.list2Tree(CopyOfTestBbsQueryList.java:94)
at edu.njcc.rj1621.client.CopyOfTestBbsQueryList.list2Tree(CopyOfTestBbsQueryList.java:94)
at edu.njcc.rj1621.client.CopyOfTestBbsQueryList.main(CopyOfTestBbsQueryList.java:28)
请大佬看看啊