浏览 2110 次
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-03-08
Exception in thread "main" java.lang.NullPointerException at abin.bbs.database.test.myforumTest.printForum2tree(myforumTest.java:57) at abin.bbs.database.test.myforumTest.printForum2tree(myforumTest.java:58) at abin.bbs.database.est.myforumTest.main(myforumTest.java:26) 我源码贴出来 请问怎么改进 import java.lang.Comparable; import java.io.Serializable; import java.util.Set; public class Forum2 implements Comparable, Serializable { private Integer id; private Set forum; private int hashCode = Integer.MIN_VALUE; public Forum2 () { initialize(); } protected void initialize () {} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Set getForum() { return forum; } public void setForum(Set forum) { this.forum = forum; } public void addToForums(Forum2 o){ if(this.forum==null) this.forum= new java.util.HashSet(); getForum().add(o); } public boolean equals (Object obj) { if (null == obj) return false; if (!(obj instanceof abin.bbs.database.test.Forum2)) return false; else { abin.bbs.database.test.Forum2 forum = (abin.bbs.database.test.Forum2) obj; if (null == this.getId() || null == forum.getId()) return false; else return (this.getId().equals(forum.getId())); } } public int hashCode () { if (Integer.MIN_VALUE == this.hashCode) { if (null == this.getId()) return super.hashCode(); else { String hashStr = this.getClass().getName() + ":" + this.getId().hashCode(); this.hashCode = hashStr.hashCode(); } } return this.hashCode; } public int compareTo (Object obj) { if (obj.hashCode() > hashCode()) return 1; else if (obj.hashCode() < hashCode()) return -1; else return 0; } public String toString () { return super.toString(); } } import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.HashSet; import abin.bbs.database.test.Forum2; [align=left]public class myforumTest { // private Forum2 Forum2; private Forum2 root; public myforumTest(){} public static void main(String[] args) { myforumTest t=new myforumTest(); //System.out.println(t.addtree().getForum().size()); t.printForum2tree(t.addtree().getForum()); //t.testtree(); } public Forum2 addtree(){ root=new Forum2(); root.setId(new Integer(0)); Forum2 child=new Forum2(); child.setId(new Integer(1)); //root.setForum(null); root.addToForums(child); for(int i=0;i<3;i++){ Forum2 childson=new Forum2(); childson.setId(new Integer(i+2)); child.addToForums(childson); } return root; } public void printForum2tree(Set node){ Iterator it=node.iterator(); while(it.hasNext()){ Forum2 fe= (Forum2)it.next(); System.out.print(fe.getId()); Set children=fe.getForum(); if(children.size()>0){ printForum2tree(children); System.out.print(" "); } } } }[align] 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-03-08
请留下各位的影子 好嘛?
|
|
返回顶楼 | |
发表时间:2008-03-08
最好格式一下代码,看的比较累,有点没看明白,
好像是private Set forum; public void setForum(Set forum) { this.forum = forum; } 但是好像没有看到那里调用了 root=new Forum2(); root.setId(new Integer(0)); Forum2 child=new Forum2(); child.setId(new Integer(1)); //root.setForum(null); root.addToForums(child); 被屏蔽le |
|
返回顶楼 | |