-1

想不通,我创建了一个简单的坐标类来保存 x 和 y 整数。在另一个类中,我有一个声明为“ords”的全局坐标数组。在我的循环中,我添加了坐标。在我的 getaction 方法中尝试使用 Coordinates 类中的方法 getX() 和 getY() 时,我得到一个空指针异常。我确定这些对象不为空,但我仍然无法弄清楚出了什么问题。任何帮助表示赞赏。

   import java.util.*;

   import org.w2mind.net.*;

   import java.io.Serializable;


   public class ConorsMind  implements Mind 
  {
     int [][] surroundings = new int [12][16];
     Coordinates [] ords = new Coordinates [192];

int currentX;
int currentY;

//====== Mind must respond to these methods: ==========================================================
//  newrun(), endrun()
//  getaction()
//======================================================================================================

public void newrun()  throws RunError 
{
}


public void endrun()  throws RunError
{
}

private void formTwoDimmensional(int [] someArray)
{
int counter = 0;
int n=0;
for(int i = 0; i < 15; i++)
    {
    for(int z = 0; z < 12; z++)
        {
            surroundings[z][i] = someArray[counter];
            if(surroundings[z][i] ==0) {
                currentX=z;
                currentY=i;
            }
            else if(surroundings[z][i]==4){
                ords[n]= new Coordinates(z,i);
                n++;
            }

            System.out.print(z+" , "+i+": "+surroundings[z][i]);
            System.out.println();
            counter++;
        }
    }
}

public Action getaction ( State state )
{ 
String  s = state.toString();        
String[]    x = s.split(",");
int act =MinerWorldUpdated.NO_ACTIONS;
int counter = 0;
int [] surround = new int [192]; 
//in this way user will have ability to see what surrounds him
for(int i = 11; i < 203; i++)
    {
    surround[counter] = Integer.parseInt(x[i]);
    counter++;
    }
    formTwoDimmensional(surround);


int [] response = new int [x.length];
for(int i = 0; i < x.length; i++)
    {
    response[i] = Integer.parseInt ( x[i] );
    }

    System.out.println("Current position: "+currentX+" ,"+currentY);


    int coalX=ords[0].getX();
    int coalY=ords[0].getY();

    System.out.println("Coal position: "+coalX+" ,"+coalY);


    if(coalX != 0 && coalY !=0)
    {
        if(coalX>currentX)
        {
            act=MinerWorldUpdated.ACTION_DOWN;
        }
        else if(coalY<currentY)
        {
            act=MinerWorldUpdated.ACTION_LEFT;
        }
        else if(coalX<currentX)
        {
            act=MinerWorldUpdated.ACTION_DOWN;
        }
        else if(coalY<currentY)
        {
            act=MinerWorldUpdated.ACTION_LEFT;
        }

    }

String a = String.format ( "%d", act );

return new Action ( a );         
}

    }

    class Coordinates implements Serializable 
    {
private int x;
private int y;

public Coordinates(int x1, int y1)
{
    x=x1;
    y=y1;
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

}

错误如下: java.lang.NullPointerException at ConorsMind.getaction(ConorsMind.java:146)

该错误源于以下两行:

int coalX=ords[0].getX();
int coalY=ords[0].getY(); 

我正在调用 formTwoDimensional() 并且它工作得很好, ords 对象正在成功创建并且不为空,因为使用 System.out.println(ords[n].getX()) 进行测试时在我的 else 中打印预期结果if(环境[z][i]==4) 块。

4

3 回答 3

2

您需要确保您正在调用 formTwoDimensional()。如果确实如此,那么您可能永远不会进入嵌套 for 循环中的 else if 块,因此 ords[0] 实际上从未被设置,因此当您尝试访问它时,它为空。

如果您不想发布其余代码,则要做的另一件事是添加更多调试代码。请参阅下面的布尔 zero_pos_set。但请确保在程序崩溃之前看到打印“Zero pos set”。我敢打赌,你不会。

public class ConorsMind  implements Mind 
{
    int [][] surroundings = new int [12][16];
    Coordinates [] ords = new Coordinates [192];
    boolean zero_pos_set = false;


    private void formTwoDimmensional(int [] someArray)
    {
        int counter = 0;
        int n=0;
        for(int i = 0; i < 15; i++) {
            for(int z = 0; z < 12; z++) {
                surroundings[z][i] = someArray[counter];
                if(surroundings[z][i] ==0) {
                    currentX=z;
                    currentY=i;
                } else if(surroundings[z][i]==4) {
                    zero_pos_set = true;
                    ords[n]= new Coordinates(z,i);
                    n++;
                }
                counter++;
            }
        }
    }

    public Action getaction ( State state ) {
        if(zero_pos_set) {
            System.out.println("Zero pos set!");
        }
        int coalX=ords[0].getX();
        int coalY=ords[0].getY();
        System.out.println("Coal position: "+coalX+" ,"+coalY);
        return new Action ( a );         
    }
}
于 2012-12-19T01:31:26.640 回答
1

根据此线程中发布的所有调试信息,似乎在您的 getaction() 函数中,您正在传递一些不包含 4 的状态。

当您解析此信息并将其传递给 formTwoDimensional() 时,您将永远不会到达 else if 块,因此永远不会设置 ords[0] 或任何其他 ords[n]。

结果,当您尝试在 getaction() 函数中重新访问 ords[0] 时,您实际上会得到 null,因此会得到 NullReferenceException。

于 2012-12-19T02:29:42.103 回答
0

这是操作顺序问题。

如果您从不调用formTwoDimmensional(),则永远不会初始化数组中的任何内容。确保您首先调用它。

当您尝试调用时,实际的 NPE 会发生,如果是coalX=ords[0].getX();,这将不起作用。ords[0]null

于 2012-12-19T01:35:19.447 回答