初始化空值项时出现空指针异常

时间:2014-11-25 03:26:12

标签: java nullpointerexception null

我想给一个名为State的类的每个子项赋值,而我有一个状态数组最初都是null,我在这里接收空指针引用:

//finding all the neighbor states of a given configuration

public State[] neighborStates(String config, int modeFlag){
    State[] neighborStates=new State[7];
    int i=0;
    for (Operation o : Operation.values()){
        neighborStates[i].config=move(config,o.name().charAt(0));
        neighborStates[i].realCost++;
        neighborStates[i].opSequence+=o.name();
        neighborStates[i].heuristicCost=getHeuristicCost(neighborStates[i].config, modeFlag);
        i++;
    }       

    return neighborStates;
}

我将代码更改为此但我还是获得了NPE:

public State[] neighborStates(String config, int modeFlag){
        State[] neighborStates=new State[8];
        int i=0;
        for (Operation o : Operation.values()){
            neighborStates[i] = new State(move(config,o.name().charAt(0)),neighborStates[i].realCost++,
                                getHeuristicCost(neighborStates[i].config, modeFlag), neighborStates[i].opSequence+=o.name());
            //neighborStates[i].config=move(config,o.name().charAt(0));
            //neighborStates[i].realCost++;
            //neighborStates[i].opSequence+=o.name();
            //neighborStates[i].heuristicCost=getHeuristicCost(neighborStates[i].config, modeFlag);
            i++;
        }

类State定义为:

public class State {
    public State(String config, int realCost, int heuristicCost, String opSequence){
        this.config = config;
        this.realCost = realCost;
        this.heuristicCost = heuristicCost;
        this.opSequence = opSequence;
    }

5 个答案:

答案 0 :(得分:7)

您需要在State数组中实例化neighborStates(s)。您创建了一个包含7个插槽的阵列,但它们最初都是null。假设你有一个默认的构造函数,它应该看起来像,

for (Operation o : Operation.values()){
  neighborStates[i] = new State();
  // ...

另外,根据neighborStates

确定Operation.values()的大小可能是个好主意
State[] neighborStates = new State[Operation.values().length];

答案 1 :(得分:0)

使用时创建指定大小的数组 State[] neighborStates = new State[7]你分配了那个记忆。无论类型的默认值是什么,都会初始化数组的每个索引。布尔值的默认值为false。数字的默认值为0。这是重要的部分:引用的默认值是null

数组中的所有值都将为null,直到您使用neighborState[i] = new State();

实例化它们为止

答案 2 :(得分:0)

当你编写这行代码时     State [] neighborStates = new State [7];
你刚收到一个对象的引用,其中7个项目都是null,这行只初始化State Array对象,里面没有任何项目。

因此在for循环中,所有项目仍然为null,您必须先使用它初始化每个项目。     neighborStates [i] = new State();

答案 3 :(得分:0)

对于您的问题版本更新后

您正在评估此表达式 -

new State(move(config,o.name().charAt(0)),neighborStates[i].realCost++,
                            getHeuristicCost(neighborStates[i].config, modeFlag), neighborStates[i].opSequence+=o.name());    

在为neighborStates[i]分配值之前。因此,neighborStates[i].realCost++表示触发空指针异常的部分 - neighborStates[i]仍为null

答案 4 :(得分:-1)

你只是初始化一个数组,但你还没有在State []数组中构造成员 在使用它之前,需要调用构造函数来构造State类。 尝试更改您的代码:

public State[] neighborStates(String config, int modeFlag){
    State[] neighborStates=new State[7];
    int i=0;
    for (Operation o : Operation.values()){
        neighborStates[i] = new State(move(config,o.name().charAt(0)),
                                      someRealCost, //what's the value of this parameter?
                                      getHeuristicCost(config, modeFlag),
                                      modeFlag
                                      );
        ....
        i++;
    }
}