Want a multidimensional array but get a null pointer exception

后端 未结 4 1513
长情又很酷
长情又很酷 2020-12-21 18:24
  1 public class TestWin{
  2     public static void main(String[] args){
  3         int n;
  4         hexagon[][] board;
  5
  6         n = 4;
  7         board          


        
相关标签:
4条回答
  • Spot on kwatford. All you have done with line 7 is to tell java to create space for n*n Hexagon objects in a 2 dimensional array.

    You will still need to call new for each of these Hexagons

    Essentially, you need to replace line 7 with something like:

    board = new Hexagon[n][n];
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            board[i][j] = new Hexagon();
    
    0 讨论(0)
  • 2020-12-21 18:43

    To expand on what kwatford said, initializing an array in java gives you nulls if the array type is an object. If you had a raw array, such as an array of doubles, you would start with 0 as the entry for each element of the array.

    0 讨论(0)
  • 2020-12-21 18:51

    Short Answer:

    As kwatford said what you need to do is this:

    board[0][0] = new hexagon(); // or whatever its constructor is
    

    Longer Explanation:

    Just to expand further. Your 2D array is; an array of pointers (or references in Java). This is what one row of the array will look like immediately after this call board = new hexagon[n][n];:

        0      1      2      3      4      5       // column index, row index = 0
    -------------------------------------------
    |   |   |   |   |   |   |      |      |      |    // value
    --- | ----- | ----- | ---------------------
        |       |       |      ...
        |       |       |
        |       |       |
        |       |       |
        |       |       v
        |       |       Null
        |       v       
        |       Null
        v
        Null (This means that it points to nothing)
    

    You tried:

    board[0][0].value = 'R';
    

    which is the same as this:

    null.value = 'R';
    

    You have initialized your array using this line:

    board = new Hexagon[n][n];
    

    But you still need to initialize the elements in your array. This would initialize the first three:

    board[0][0] = new hexagon(); // or whatever its constructor is
    board[1][0] = new hexagon(); // or whatever its constructor is
    board[2][0] = new hexagon(); // or whatever its constructor is
    

    Which would result in an array that looks like this:

        0      1      2      3      4      5       // column index, row index = 0
    -------------------------------------------
    |   |   |   |   |   |   |      |      |      |    // value
    --- | ----- | ----- | ---------------------
        |       |       |
        |       |       |
        |       |       |
        |       |       |
        |       |       v
        |       |       An instance of type Hexigoon (what you get when you type new Hexigon)
        |       v       
        |       An instance of type Hexigon (what you get when you type new Hexigon)
        v
        An instance of type Hexigon (what you get when you type new Hexigon)
    

    I remember banging my head on the table with this exact problem two years ago. I love stackoverflow

    0 讨论(0)
  • 2020-12-21 18:59

    It's been a while since I've so much as looked at Java, but have you tried doing this first?

    board[0][0] = new hexagon(); // or whatever its constructor is
    
    0 讨论(0)
提交回复
热议问题