简体   繁体   中英

Null Pointer Exception - Printing 2d array

I'm having an issue with attempting to print a 2 dimensional array, I continually get a nullpointer exception at the first for loop in printArray(), and at printArray(). I do know what a nullpointerexception means, I'm just having a hard time understanding why I'm getting it. I've ran into this problem before while trying to print an array and it'd be great for me to finally figure this out.

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;

is null when you call it

you have never initialized the array, nor put anything into it

The problem lies in the test condition of printArray() function:

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

Here, unitest is a null object and when you try to apply length method on it, its throwing and exception.

Basically, you are not initializing the unittest object (2D array in your case). You can do something like this to avoid the exception:

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

Hope it helps :)

Beyond initializing the array you have an off by one error

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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