2012-12-19 86 views
-1

無法弄清楚這一點,我創建了一個簡單的類來保存x和y整數。在另一個類中,我有一個叫做「ords」的全局座標數組。在我的循環中,我添加了座標。在我的getaction方法中嘗試使用座標類中的方法getX()和getY()時,我得到一個空指針異常。我確信這些對象不是空的,但我仍然無法弄清楚什麼是錯誤的。任何幫助讚賞。Java - NullPoinerException對象數組

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 在ConorsMind.getaction(ConorsMind.java:146)

該錯誤是由以下兩行詞幹:

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

我打電話給formTwoDimensional(),它的工作完美,ords對象正在成功創建nd不爲null,因爲使用System.out.println(ords [n] .getX())進行測試時,將放置在我的else if(surround [z] [i] == 4)塊中時會打印預期結果。

+0

請指出您在哪裏得到異常。 – Thilo

+0

分享你的堆棧跟蹤。 – Smit

+0

請發佈異常的堆棧跟蹤。 –

回答

2

你需要確保你調用了formTwoDimensional()。如果確實如此,那麼很可能你沒有進入嵌套for循環的else塊,因此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);   
    } 
} 
+0

我沒有包含完整的代碼,因爲它太大了,但是我正在調用formTwoDimensional()並且對象正在成功初始化。打印出ords [0] .getY();在我的其他如果(環境[z] [i] == 4)塊正常工作,我得到正確的結果,但它不工作在int coalX = ords [0] .getX();在我的getaction方法中 – derpyderp

+1

您是否在調用getAction()的同一個ConorsMind實例上調用formTwoDimensional()?你確定你在getAction()之前調用了formTwoDimensional()嗎? – TimoteeTheCodeMonkee

+0

先不調用getaction,然後在getaction中調用formTwoDimensional()。在formTwoDimensional()被調用後,int coalX和int coalY被啓動。 – derpyderp

0

這是操作順序問題。

如果您從未打電話給formTwoDimmensional(),您將永遠不會初始化陣列內部的任何內容。確保你先打電話。

當您嘗試撥打coalX=ords[0].getX();時,實際的NPE會發生,如果ords[0]null,則不會工作。

+0

...或者_getX()_爲空(因爲它被分配給一個原始的int)。 – jahroy

+0

然後它將返回0.這是空值的座標實例。 – TimoteeTheCodeMonkee

+0

糟糕...你是對的。沒有注意到getX()返回了一個原始的int。我的錯。 – jahroy

1

基於所有這些線程內張貼的調試信息,似乎在你的getAction()函數,你傳遞一些狀態,即不包含4

當你解析這一信息並將其傳遞給formTwoDimensional(),你將永遠不會到達else if塊,所以ords [0]或其他ords [n]永遠不會被設置。因此,當您嘗試訪問您的getaction()函數中的ords [0]時,實際上您會得到null,因此您的NullReferenceException。

+0

+1因爲有耐心堅持這一點,儘管OP讓它很難提供幫助。 – jahroy

+0

謝謝。我記得當我第一次學習編程的時候,當我努力理解某些概念時,我多麼感謝幫助,等等。 – TimoteeTheCodeMonkee