public class TestCount{
public static void main(String[] args){
kidCicrle kc = new kidCicrle(0);
int countNum = 0;
kid k = kc.first;
while(kc.count >1){
countNum++;
if(countNum == 3){
countNum = 0;
kc.delete(k);
}
k = k.right;
}
System.out.println(kc.first.id);

}
}class kid{
int id;
kid right;
kid left;}class kidCicrle{
int count = 0;
kid first,last; kidCicrle(int n){
for(int i = 0;i<n;i++){
add();
}

}
void add(){
kid k = new kid();
k.id = count;
  if(count<=0){
first = k;
last = k;
k.right = k;
k.left = k;
}
else{
last.right = k;
k.right = first;
last = k;
}
count++;
}void delete(kid k){
if(count <= 0){
System.out.println("error");
}else if(count == 1){
first = last = null;
}
else{
k.left.right = k.right;
k.right.left = k.left;
if(k == first){
first = k.right;
}
if(k == last){
last = k.left;
}
}
count--;
}
}

程序编译能通过,但运行时却抛出异常
Exceprion in thread "main" java.lang.NullpointerException
     at kidCicrle.delete<TestCount.java:62>
     at TesrCount.main<TestCount.java:10>

解决方案 »

  1.   

    Exceprion in thread "main" java.lang.NullpointerException
      at kidCicrle.delete<TestCount.java:62>这两句话,你没考虑有可能 k.left为null 以及 k.right为null 的特殊情况:
    k.left.right = k.right;
    k.right.left = k.left;
      

  2.   


    public class TestCount {
    public static void main(String[] args) {
    kidCicrle kc = new kidCicrle(20);
    int countNum = 0;
    kid k = kc.first;
    while (kc.count>0&&k!=null){//加k!=null约束
    countNum++;
    if (countNum == 3){
    countNum = 0;
    kc.delete(k);
    }
    k = k.son;
    }
    for(kid k1 = kc.first;k1 != null;k1 = k1.son){//链表遍历
    System.out.print(k1.id +" ");
    }
    }
    }
    class kid {
    int id = 0;
    kid father = null;
    kid son = null;
    }class kidCicrle {
    int count = 0;
    kid first = null;
    kid last = null;
    kidCicrle(int n) {
    for (int i = 0;i<n;i++){//你赋值为0,对照你自己的程序
    add();
    }
    }

    void add() {
    kid k = new kid();
    k.id = count;
    if (count==0){
    first=k;
    last=k;
    first.son = last;
    last.father = first;
    }
    else{
    k.father = last;//注意双向都要连,避免照成空指针异常
    last.son = k;
    last = k;
    }
    count++;
    }
    void delete(kid k){
    if (count <= 0){
    System.out.println("error");
    }else if (count==1){
    first = null;
    last = null;
    }else {//双向链表,把引用搞清楚。
    if (k==first){
    k.son.father = null;
    first = k.son;
    }
    else if (k==last){
    k.father.son=null;
    last = k.father;
    }
    else{
    k.son.father = k.father;
    k.father.son = k.son;
    }
    }
    count--;
    }
    }
      

  3.   

    用if处理掉就行了,链表算法,一般就两个关注点:
    1、只剩一个元素或根本没元素的时候,执行 delete;
    2、没有任何元素的时候,执行 add;if (k.left != null) ......
      

  4.   

    楼主具体什么问题,我感觉你程序错误多,就重构了一下。程序乱,不严谨。还有双向链表确实要小心点用。
    kidCicrle kc = new kidCicrle(0);这句报空指针错误,是因为kc==null。
    所以后面报错。所以加个判断就好了 ,main方法第三行
    if(kc==null) return;
    把你的right ,left 改成 father,son了,便于区分。
      

  5.   


    public class TestCount {
        public static void main(String[] args) {
            kidCicrle kc = new kidCicrle(20);
            if(kc==null||kc.first==null){
             System.out.println("kc为空,系统退出");
             System.exit(0);
            }
            int countNum = 0;
            kid k = kc.first;
            while (kc.count>0&&k!=null){//加k!=null约束
                countNum++;
                if (countNum == 3){
                    countNum = 0;
                    kc.delete(k);
                }
                k = k.son;
            }
            for(kid k1 = kc.first;k1 != null;k1 = k1.son){//链表遍历
                System.out.print(k1.id +" ");
            }
        }
    }
    class kid {
        int id = 0;
        kid father = null;
        kid son = null;
    }class kidCicrle {
        int count = 0;
        kid first = null;
        kid last = null;
        kidCicrle(int n){
            for (int i = 0;i<n;i++){//你赋值为0,对照你自己的程序
                add();
            }
        }
        
        void add() {
            kid k = new kid();
            k.id = count;
            if (count==0){
                first=k;
                last=k;
                first.son = last;
                last.father = first;
            }
            else{
                k.father = last;//注意双向都要连,避免照成空指针异常
                last.son = k;
                last = k;
            }
            count++;
        }
        void delete(kid k){
            if (count <= 0){
                System.out.println("error");
            }else if (count==1){
                first = null;
                last = null;
            }else {//双向链表,把引用搞清楚。
                if (k==first){
                    k.son.father = null;
                    first = k.son;
                }
                else if (k==last){
                    k.father.son=null;
                    last = k.father;
                }
                else{
                    k.son.father = k.father;
                    k.father.son = k.son;
                }
            }
            count--;
        }
    }
    //这次要是还会报错,我就无话好说了。
    运行结果
    0 1 3 4 6 7 9 10 12 13 15 16 18 19
      

  6.   

    public class TestCount{
    public static void main(String[] args){
    kidCicrle kc = new kidCicrle(500);
    int countNum = 0;
    kid k = kc.first;
    while(kc.count >1){
    countNum++;
    if(countNum == 3){
    countNum = 0;
    kc.delete(k);
    }
    k = k.right;
    }

    System.out.println(kc.first.id);

    }
    }class kid{
    int id;
    kid right;
    kid left;}class kidCicrle{
    int count = 0;
    kid first,last; kidCicrle(int n){
    for(int i = 0;i<n;i++){
    add();
    }

    }
    void add(){
    kid k = new kid();
    k.id = count;
      if(count<=0){
    first = k;
    last = k;
    k.left = k;
    k.right = k;
    }
    else{
    last.right = k;
    k.left = last;
    k.right = first;
    first.left = k;
    last = k;
    }
    count++;
    }void delete(kid k){
    if(count <= 0){
    System.out.println("error");
    }else if(count == 1){
    first = last = null;
    }
    else {
    k.left.right = k.right;
    k.right.left = k.left;
    if(k == first){

    first = k.right;
    }
    else if(k == last){

    last = k.left;
    }
    }
    count--;
    }
    }
    编译结果是 345