Exception in thread "main" java.lang.NullPointerException……………………在线等。。。。。。。

Diode_XP 2008-12-13 10:55:17
写了一个关于链表的简单程序,出现RT的异常,高手指点一下。先谢了。代码如下:
class Person{
public String name;
public void setname(String b){
name=b;
}
String getname(){
return name;
}
}
class Node {
public Person person;
public Node head;
public Node next;
public boolean isEmpty(){
return head==null;
}//判断链表是否为空
public void printAll(){
Node tmp=head;
if(isEmpty())
System.out.println("这是一个空链表");
while(tmp!=null){
System.out.print(tmp.person.getname()+" ");
tmp=tmp.next;
}
}//遍历链表打印结果

}

public class LianBiao {
public static void main(String [] args){
Node nod=new Node();
nod.person.setname("qq");
nod.next=new Node();
nod.head=nod;
Node tem=new Node();
tem=nod.next;
tem.person.setname("qw");
nod.printAll();
}
}
...全文
81 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahlon 2008-12-14
  • 打赏
  • 举报
回复
Node nod=new Node();的时侯里面的person字段没有初始化
建议给Node类加个构造方法
public Node() {
person = new Person();
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧