1
  1 public class TestWin{
  2     public static void main(String[] args){
  3         int n;
  4         hexagon[][] board;
  5
  6         n = 4;
  7         board = new hexagon[n][n];
  8         board[0][0].value = 'R';

Hi. javac doesn't like what I did on line 8. Does anyone know why?

3
  • Agree. We know nothing about the hexagon type and what <Hexagon>.value means Commented Jul 30, 2010 at 1:41
  • Wtf is .value? It's not VBA :) Commented Jul 30, 2010 at 12:37
  • @SebastienLorber: .value() would be a method that is defined in the Hexagon class. Commented Jul 31, 2010 at 13:06

4 Answers 4

10

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
Sign up to request clarification or add additional context in comments.

Comments

8

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

Comments

5

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.