-1

これを理解することはできません、私はxとyのintを保持するための座標の単純なクラスを作成しました。別のクラスでは、「ords」と呼ばれる宣言された座標のグローバル配列があります。私のループでは、座標を追加しています。getactionメソッドでCoordinatesクラスのメソッドgetX()とgetY()を使用しようとすると、nullポインター例外が発生します。オブジェクトがnullではないことは確かですが、それでも何が問題になっているのか理解できません。助けていただければ幸いです。

   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;
}

}

エラーは次のとおりです。ConorsMind.getaction(ConorsMind.java:146)でのjava.lang.NullPointerException

このエラーは、次の2行に起因しています。

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

私はformTwoDimensional()を呼び出しており、その動作は完全です。ordsオブジェクトは正常に作成されており、System.out.println(ords [n] .getX())でテストすると、elseに配置されたときに期待される結果が出力されるためnullではありません。 if(surroundings [z] [i] == 4)ブロック。

4

3 に答える 3

2

formTwoDimensional() を呼び出していることを確認する必要があります。そうであれば、入れ子になった for ループの else if ブロックに入ることがない可能性が高いため、 ords[0] が実際に設定されることはないため、アクセスしようとすると null になります。

残りのコードを投稿したくない場合は、さらにデバッグ コードを追加する必要があります。以下のブール値 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 が発生しますが、 is の場合はcoalX=ords[0].getX();機能しません。ords[0]null

于 2012-12-19T01:35:19.447 に答える