2014-10-08 29 views
0

我有一個試圖打印2維數組的問題,我不斷地在printArray()和printArray()的第一個for循環中得到一個空指針異常。我確實知道什麼是虛假的感覺,我只是很難理解爲什麼我會得到它。在嘗試打印數組之前,我遇到過這個問題,最後我會很好地理解這一點。空指針異常 - 打印二維數組

import java.lang.*; 
import java.util.*; 

public class Board { 

int N = 3; 
static int [][] unittest; 
//construct a board from an N-by-N array of blocks 
//(where blocks[i][j] = block in row i, column j) 
public Board(int[][] blocks){ 
    blocks = new int[N][N]; //creates array of size N 
    //generates random numbers 0 inclusive to # exclusive 
    //Random r = new Random(); uses blocks[i][j] = r.nextInt(); 
    for (int i = 0; i < blocks.length; i++){ 
     for (int j = 0; j < blocks[i].length; j++){ 
      blocks[i][j] = randInt(0,9); 
     } 
    } 
    unittest = blocks.clone(); 
} 
//generates random numbers in a range 
private static int randInt(int min, int max){ 
    Random rand = new Random(); 
    int randomNum = rand.nextInt((max - min) + 1) + min; 
    return randomNum; 
} 
//board size N 
public int size(){ 
    return 0; //placeholder 
} 
//number of blocks out of place 
public int hamming(){ 
    return 0; //placeholder 
} 
//sum of manhattan distances between blocks and goal 
public int manhattan(){ 
    return 0; 
} 
//is this board the goal board? 
public boolean isGoal(){ 
    return true; //placeholder 
} 
//is the board solvable? 
public boolean isSolvable(){ 
    return true; //placeholder 
} 
//does this board equal y? 
//Object because can only take objects 
//does it use Comparable? 
public boolean equals(Object y){ 
    return true; //placeholder 
} 
//all neighboring boards 
public Iterable<Board> neighbors(){ 
    Stack<Board> placeholder = new Stack<Board>(); 
    return placeholder; 
} 
//string representation of the board 
public String toString(){ 
    return "placeholder"; 
} 
//unit test 

private static void printArray(){ 
    for (int i = 0; i < unittest.length; i++){ //**NULL POINTER EXCEPTION 
     for (int j = 0; j < unittest[i].length; j++){ 
      System.out.print(unittest[i][j]); 
      if (j < unittest[i].length - 1){ 
       System.out.print(" "); 
      } 
     } 
     System.out.println(); 
    } 

} 
public static void main(String[] args){ 
    //prints out board 
    printArray(); //**NULL POINTER EXCEPTION 


} 

}

+0

初始化你有一個關閉數組你需要作出一個實例你的班級的關鍵字'new'。做到這一點主要。這樣你的變量'unittest'將是非空的,因爲你的類的構造函數是'unittest'的填充。 – csmckelvey 2014-10-08 03:18:30

回答

0

的問題出在測試條件printArray()功能:

for (int i = 0; i < unittest.length; i++) 

這裏,unitest是一個空對象,當您嘗試對其應用長度方法時,它的引發和異常。

基本上,你沒有初始化unittest對象(在你的情況下是2D數組)。你可以做這樣的事情,以避免該異常:

private static void printArray(){ 
    if(unittest == null) 
     System.out.println("its NULL"); 
    else{ 
     for (int i = 0; i < unittest.length; i++){ //**NULL POINTER EXCEPTION 
      for (int j = 0; j < unittest[i].length; j++){ 
       System.out.print(unittest[i][j]); 
       if (j < unittest[i].length - 1){ 
       System.out.print(" "); 
       } 
      } 
      System.out.println(); 
     } 
    } 
} 

希望它能幫助:)

0
static int [][] unittest; 

爲空時,你怎麼稱呼它

你從來沒有初始化數組,也沒有將任何東西放入它

0

超出了一個錯誤

public Board(int[][] blocks){ 
    blocks = new int[N][N]; //creates array of size N 
    //generates random numbers 0 inclusive to # exclusive 
    //Random r = new Random(); uses blocks[i][j] = r.nextInt(); 

    for (int i = 0; i < blocks.length; i++){    <----------------- blocks length should be blocks.length-1 

     for (int j = 0; j < blocks[i].length; j++){  <---------------------also blocks [i].length - 1 
      blocks[i][j] = randInt(0,9); 
     } 
+0

如果數組長度爲10,則循環從0-9變爲9(因爲9 <10),這很好。你的「off-by-one」錯誤是錯誤的。如果循環使用'<=',那麼你會是正確的。 – csmckelvey 2014-10-08 05:55:14