关于Java中NullPointerException异常的请教


public class Distribution {
    private Idiot [] idiot;
    public Distribution(){
        this.idiot=new Idiot[5];
        Chopsticks [] cp = new Chopsticks[5];
        for(int i=0; i<5;i++){
            this.idiot[i].setLEFT_CHOPSTICKS(cp[i]);   //报错是这里有问题
            if(i == 0){
                this.idiot[i].setRIGHT_CHOPSTICKS(cp[4]);
            }else{
                this.idiot[i].setRIGHT_CHOPSTICKS(cp[i-1]);
            }
        }
    }
    public static void main(String [] args)
    {
        Distribution distribution = new Distribution();
    }
}

 public class Idiot {
    private Chopsticks LEFT_CHOPSTICKS=null;
    private Chopsticks RIGHT_CHOPSTICKS=null;
    public Chopsticks getLEFT_CHOPSTICKS() {
        return LEFT_CHOPSTICKS;
    }
    public void setLEFT_CHOPSTICKS(Chopsticks left_chopsticks) {
        LEFT_CHOPSTICKS = left_chopsticks;
    }
    public Chopsticks getRIGHT_CHOPSTICKS() {
        return RIGHT_CHOPSTICKS;
    }
    public void setRIGHT_CHOPSTICKS(Chopsticks right_chopsticks) {
        RIGHT_CHOPSTICKS = right_chopsticks;
    }

}

 public class Chopsticks {
    private boolean USING_STATE;

    public Chopsticks(){
        this.USING_STATE=false;
    }

    public void setUSING_STATE(boolean using_state) {
        USING_STATE = using_state;
    }

    public boolean isUSING_STATE() {
        return USING_STATE;
    }

}
 Exception in thread "main" java.lang.NullPointerException
    at Distribution.<init>(Distribution.java:8)
    at Distribution.main(Distribution.java:18)

请问是哪里有问题 该怎么改

idiot和cp都没有初始化吧

this.idiot没有初始化,this.idiot[i]为NULL,所以调用setLEFT_CHOPSTICKS方法时,
=NULL.setLEFT_CHOPSTICKS
所以抛出java.lang.NullPointerException。

追加输出log

 public class Distribution {
    private Idiot[] idiot;

    public Distribution() {
        this.idiot = new Idiot[5];
        Chopsticks[] cp = new Chopsticks[5];

        // 追加的代码 Begin
        for (int i = 0; i < 5; i++) {
            System.out.println("cp[" + i + "]=" + cp[i]);
        }

        for (int i = 0; i < 5; i++) {
            System.out.println("this.idiot[" + i + "]=" + this.idiot[i]);
        }
        // 追加的代码 End

        for (int i = 0; i < 5; i++) {
            this.idiot[i].setLEFT_CHOPSTICKS(cp[i]);
            if (i == 0) {
                this.idiot[i].setLEFT_CHOPSTICKS(cp[4]);
            } else {
                this.idiot[i].setLEFT_CHOPSTICKS(cp[i - 1]);
            }
        }
    }

    public static void main(String[] args) {
        Distribution distribution = new Distribution();
    }
}
    ```

执行结果

cp[0]=null
cp[1]=null
cp[2]=null
cp[3]=null
cp[4]=null
this.idiot[0]=null
this.idiot[1]=null
this.idiot[2]=null
this.idiot[3]=null
this.idiot[4]=null
Exception in thread "main" java.lang.NullPointerException
at Distribution.(Distribution.java:19)
at Distribution.main(Distribution.java:29)


具体你要实现的业务逻辑我不知道,单从解决java.lang.NullPointerException来说,
你可以对this.idiot进行初始化。

public class Distribution {
private Idiot[] idiot;

public Distribution() {
    this.idiot = new Idiot[5];

    Chopsticks[] cp = new Chopsticks[5];

    // 追加的代码 Begin
    for (int i = 0; i < 5; i++) {
        System.out.println("cp[" + i + "]=" + cp[i]);
    }

    for (int i = 0; i < 5; i++) {
        System.out.println("this.idiot[" + i + "]=" + this.idiot[i]);
    }
    // 追加的代码 End

    // 对idiot初始化 Begin
    for (int i = 0; i < idiot.length; i++) {
        idiot[i] = new Idiot();
        System.out.println("this.idiot[" + i + "]=" + this.idiot[i]);
    }
    // 对idiot初始化 End

    for (int i = 0; i < 5; i++) {
        this.idiot[i].setLEFT_CHOPSTICKS(cp[i]);
        if (i == 0) {
            this.idiot[i].setLEFT_CHOPSTICKS(cp[4]);
        } else {
            this.idiot[i].setLEFT_CHOPSTICKS(cp[i - 1]);
        }
    }
}

public static void main(String[] args) {
    Distribution distribution = new Distribution();
}

}

输出结果


cp[0]=null
cp[1]=null
cp[2]=null
cp[3]=null
cp[4]=null
this.idiot[0]=null
this.idiot[1]=null
this.idiot[2]=null
this.idiot[3]=null
this.idiot[4]=null
this.idiot[0]=Idiot@15db9742
this.idiot[1]=Idiot@6d06d69c
this.idiot[2]=Idiot@7852e922
this.idiot[3]=Idiot@4e25154f
this.idiot[4]=Idiot@70dea4e