当前位置: 代码迷 >> Java相关 >> java程序问题求助??
  详细解决方案

java程序问题求助??

热度:413   发布时间:2007-09-25 17:20:15.0
java程序问题求助??
这是我编的一个java链表,但是在调试时总是出现以下提示:
import java.io.*;
import java.lang.*;
public class Lisk1{
private Node Head=null;
private Node Tail=null;
private Node Pointer;
private int Length=0;
void insert(Lisk1 a,Node e)
{
a.Length++;
if(a.Length==1)
{
a.Head=e;
a.Tail=a.Head;
a.Pointer=a.Head.next;
}
else
{
a.Tail.next=e;
a.Tail=e;
a.Pointer=e.next;
}
}
void output(Lisk1 a)
{
a.Pointer=a.Head;
for(int i=1;i<=a.Length;i++){
System.out.print(Pointer.data+" ");
a.Pointer=a.Pointer.next;
if(a.Pointer==null)
throw new java.lang.NullPointerException();
}
}
void delete(Lisk1 a,int j){
int i=1;
a.Pointer=a.Head;
if(j==1){
System.out.println("The chracter is"+" "+a.Pointer.data);
a.Head=a.Pointer.next;
}
else
{
while(i!=j-1)
{
a.Pointer=a.Pointer.next;
i++;
}
System.out.println("The chracter is"+" "+a.Pointer.next.data);
if(j==10)
a.Tail=a.Pointer;
a.Pointer=a.Pointer.next.next;

}
}
public static void main(String[] args){
int i;
int j=2;
Lisk1 a=new Lisk1();
for(i=0;i<10;i++){
Node e=new Node(new Integer(i));
a.insert(a,e);
}
System.out.println("Please output the characters of link:");
a.output(a);
System.out.println("Please delete the index");
a.delete(a,j);
}
}
class Node{
Object data;
Node next;
public Node(Object d){
data=d;
next=null;
}
}
提示:Exception in thread "main"java.lang.NullPointerException at Lisk1.output<Lisk1.java:31>
at Lisk1.main<Lisk1.java:64>

望各位人兄帮帮忙!!!!
搜索更多相关的解决方案: java  

----------------解决方案--------------------------------------------------------

我用 Eclipse 编译是这样的,你这个异常 NullPointerException(); 没有定义,只是抛出,没有catch.
我定义了一下,输出这样的结果:
Please output the characters of link:
0 1 2 3 4 5 6 7 8 9 please delete the index
The chracter is 1

/* 修改的地方 */
void output(Lisk1 a)
{
try{
a.Pointer=a.Head;
for(int i=1;i<=a.Length;i++)
{
System.out.print(Pointer.data+" ");
a.Pointer=a.Pointer.next;
if(a.Pointer==null)
throw new java.lang.NullPointerException();
}
}catch(NullPointerException e ) {}

}




----------------解决方案--------------------------------------------------------

呵呵.太厉害了啊


----------------解决方案--------------------------------------------------------
  相关解决方案