3

I've got an array in an array and want to initialize it with a for each loop.

// class variable  
Tree[][] trees;

// in constructor

this.trees = new Tree[length][with];

// initialize
for (Tree[] tree : this.trees){
    for(Tree tree2 : tree){
    tree2 = new Tree();
    System.out.println(tree2);
    }
}

for (Tree[] tree : this.trees) {
    for (Tree tree2 : tree) {
    System.out.println(tree2);
    }
}

What happens is that the first println prints initialized trees, so they got initialized. I thought everything is ok. But when I try to use these trees, I get a nullpointerexception. So I tried to loop through the arrays again, and the second println gives me null for every tree. How can this be? What am I missing here? Thank you!

Edit: Oh I'm very sorry, It wasn't the main but the constructor method the loops are placed.

1
  • 4
    you can't use this in the main method, because main is static. Also, this code tree2 = new Tree(); has no effect at all, because tree2 is just a local variable in the loop. There is no way to initialize objects the way you want with a for each loop. Commented Jan 24, 2013 at 9:53

4 Answers 4

3

tree2 is a local variable (available in the scope the loop), you assign the newly created Tree instance to that variable not to your array. Next you print the contents of that variable so it seems to work...

Instead you need to explicitly store the instance into trees, like this:

for (int l = 0; l < length; l++){
  for(int h = 0; h < height; h++) {
    trees[l][h] = new Tree();
    System.out.println(trees[l][h]);
  }
}

As you can see, it stores the instance in the array and uses the array to print the value.

Sign up to request clarification or add additional context in comments.

Comments

2

As per my comment above, this:

for (Tree[] tree : this.trees){
    for(Tree tree2 : tree){
    tree2 = new Tree(); // changes tha variable tree2, does not touch the array.
    System.out.println(tree2);
    }
}

has no effect. You need

for (int i = 0; i < length; i++) {
    for (int j = 0; j < width; j++) {
        trees[i][j] = new Tree(); // obviously changes the array content.
    }
}

Comments

1

Your problem is in the first loop:

tree2 = new Tree();

This line indeed creates instance of Tree but it stores it in local variable tree2 instead of in element of array this.trees.

You should iterate over array using inexed for loop:

for (int i = 0; i < trees.length; i++) {
    for (int j = 0; j < trees[i].length; j++) {
        threes[i][j] = new Tree();
    }
}

The difference between lines threes[i][j] = new Tree(); and tree = new Tree(); is that first stores instance in element of array and the second stores it in single variable.

Java references are not C pointers. The assignment of objects is assignment of reference by value.

Comments

1

in this for loop

for(Tree tree2 : tree)
{
    tree2 = new Tree();

you are doing the assignment to the local reference variable tree2, where as tree[i][j] doesn't get assigned any value.

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.