繁体   English   中英

空指针异常-打印二维数组

[英]Null Pointer Exception - Printing 2d array

我在尝试打印二维数组时遇到问题,我在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 


}

}

static int [][] unittest;

调用时为null

您从未初始化过数组,也没有放入任何东西

问题在于printArray()函数的测试条件:

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

在这里,unitest是一个空对象,当您尝试在其上应用length方法时,它会引发异常。

基本上,您不是在初始化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();
        }
     }
}

希望能帮助到你 :)

除了初始化数组之外,您还会遇到一个错误

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM