空指针异常 - 打印2d数组

时间:2014-10-08 03:15:07

标签: java arrays exception nullpointerexception null-pointer

我遇到了尝试打印二维数组的问题,我在printArray()和printArray()的第一个for循环中不断得到一个nullpointer异常。我确实知道nullpointerexception意味着什么,我只是很难理解为什么我会得到它。我在尝试打印数组之前遇到过这个问题,对我来说最好的解决这个问题真的很棒。

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 


}

}

3 个答案:

答案 0 :(得分:0)

static int [][] unittest;
调用它时

为null

你从来没有初始化数组,也没有把任何东西放进去

答案 1 :(得分: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();
        }
     }
}

希望有所帮助:)

答案 2 :(得分: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);
        }