簡體   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