0
public class Gardener {


private int amountFlowerbed;

Gardener(int amountFlowerbed){

    this.amountFlowerbed=amountFlowerbed;
}
Flowerbed[] flowerbeds = new Flowerbed[amountFlowerbed];

public void work() {
    Watering_machine wateringMachine = new Watering_machine();

    Thread[] threads = new Thread[amountFlowerbed];
    for (int i=0;i<amountFlowerbed;i++) {
        flowerbeds[i]=new Flowerbed(wateringMachine,i+1);
        threads[i] = new Thread(flowerbeds[i]);
        threads[i].start();
    }
    }
}

When i initialize array flowerbeds with amountFlowerbed i get this Exception, but if i do it with some concrete value (for example 2) it works fine. Herewith amountFlowerbed contains same value. And most strange that threads array initialized with amountFlowerbed

Sergey
  • 421
  • 1
  • 5
  • 14
  • 1
    While you don't show it here, possibly the value of `amountFlowerbed` is changing between the creation of the array and the execution of the `for` loop. – markspace May 26 '16 at 18:22
  • @markspace it only initialized in constructor – Sergey May 26 '16 at 18:24
  • 1
    Only reason I can think of: `amountFlowerbed` is not set to a value greater than 0 _at that time_, but when work is called it is. I assume `amountFlowerbed` is a member variable - where do you set it? In the constructor? That would be too late. You would have to set it at declaration to be set at time. – Tobias Brösamle May 26 '16 at 18:24
  • 2
    Well, show us the complete code, constructor included. – markspace May 26 '16 at 18:25
  • 2
    "When i initialize array flowerbeds with amountFlowerbed" you are not initializing array with `amountFlowerbed` variable, but with value which was held by that variable at the moment of executing `new Flowerbed[amountFlowerbed]` which happens at start of each constructor. So if later you will change value of `amountFlowerbed` size of array will not change. Anyway based on error message it seems that `amountFlowerbed` held `0` value when you created array. – Pshemo May 26 '16 at 18:25
  • 1
    Well, I tried to close this question as the dupe of this one: [Are fields initialized before constructor code is run in Java?](http://stackoverflow.com/q/14805547), but I was too late :D. – Tom May 26 '16 at 18:34
  • 1
    @Tom I agree that this is better duplicate since problem is not that exception happened but why it happened and this case is not that trivial as in previous duplicate. I changed duplicate target. – Pshemo May 26 '16 at 18:37
  • @TobiasBrösamle you right. I moved initialization in function and all worked. – Sergey May 26 '16 at 18:39
  • Do you understand what was causing the problem or do you need farther explanation? – Pshemo May 26 '16 at 18:41
  • @Pshemo i understand – Sergey May 26 '16 at 18:44
  • Anyway for future reader I will try to add few details. When we initialized field while declaration, like `y` in `class Foo{ int x; int y=1; public Foo(int x){this.x=x;}}` then initialization code of our fields `x` and `y` will be moved to each constructor of our class. It will also be executed between its `super` call (explicit or implicit) and code which we added after it. This means that our class is same as `class Foo{int x; int y; public Foo(int x){super(); x=0;/*default value*/ y=1;/*value we set when declaring*/ this.x=x;}}`. – Pshemo May 26 '16 at 18:49
  • So here `amountFlowerbed` was like `x` and it was in constructor set do `0` (default value of each primitive numeric type), then `Flowerbed[] flowerbeds = new Flowerbed[amountFlowerbed];` used that `0` value to create array. So solution was simply not initializing `flowerbeds` while declaration, but do it inside constructor, when we will know proper value passed to it. – Pshemo May 26 '16 at 18:51

0 Answers0