# Problem with a loop

**URL:** <https://discourse.processing.org/t/problem-with-a-loop/3415>\
**Category:** Coding Questions\
**Created:** [September 10, 2018, 11:02am UTC](https://discourse.processing.org/t/problem-with-a-loop/3415 "2018-09-10T11:02:15Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [September 11, 2018, 12:12pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/21 "2018-09-11T12:12:40Z")

</div>

i use an iterator for the food2 because i had an error when i use the same template

well i dont initialize the position because i dont know how (i was thinking thats what i do when i add them with grow.  
I was thinking the return position was fine, its work when my creatures eat it on the ground randomly.

what do you mean by `ArrayList&lt;PVector&gt;` ? and yes i surely mix some thing 🙂

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 11, 2018, 12:23pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/22 "2018-09-11T12:23:51Z")

</div>

> [@vincentstrebelle](#):
>
> I was thinking the return position was fine

Its not that it is wrong, it’s just that you don’t really need it. You need “getter” and “setter” function when your variables are private; and they should be to be compliant with the idea of encapsulation. In your case, the variable is public so you can access it directly without using a function.

> [@vincentstrebelle](#):
>
> i was thinking thats what i do when i add them with grow

You are not setting the position variable, you are adding a new PVector to your food2 arrayList. So your position variable stays undetermined.

> [@vincentstrebelle](#):
>
> what do you mean by `ArrayList&lt;PVector&gt;` ?

I meant `ArrayList<PVector> food2;`. Something went wrong with the copy/pasting. I ask that because you have a class Food2 that should represent your food and that have a position. But there is also that list that seems to also be food but that is just PVector object and not Food2 object.

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [September 11, 2018, 12:31pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/23 "2018-09-11T12:31:09Z")

</div>

for the `ArrayList<PVector> food2;` i have that with all my class, if i dont put that line i got an java null pointer exception.

for the position i think i get what you mean but i dont know how to fix it

i think its because i mix java and the Processing library

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [September 11, 2018, 12:33pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/24 "2018-09-11T12:33:23Z")

</div>

ok correction i have it also in my creature class but it does nothing i just tried

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 11, 2018, 12:40pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/25 "2018-09-11T12:40:12Z")

</div>

> [@vincentstrebelle](#):
>
> for the `ArrayList<PVector> food2;` i have that with all my class, if i dont put that line i got an java null pointer exception.

It seems odd but as long as it works for you. I would maybe try to rethink the design a bit though.  
It seems that what you want is an array of Food2 in your word and the grow function should add Food to that array and not inside the Food2 object.

Anayway, to fix your problem Try the following:

```auto
PVector target = new PVector(0,0);
currentSmallest = (parent.height * parent.height) + (parent.width * parent.width);
				
for(Iterator<Food2> i = food2.iterator(); i.hasNext();){
  Food2 f = i.next();
  ArrayList<PVector> foodList = f.getFood2()

  for (int i = 0; i < foodList.size(); i++) {
    PVector food = foodList.get(i);
    dist = ((h3f.position.x - food.x) * (h3f.position.x - food.x)) + ((h3f.position.y - food.y) * (h3f.position.y - food.y));

    if(dist < currentSmallest){
      currentSmallest = dist;
      target.x = f.position.x;
      target.y = f.position.y;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [September 11, 2018, 12:52pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/26 "2018-09-11T12:52:16Z")

</div>

> [@jb4x](#):
>
> I would maybe try to rethink the design a bit though.

yes but im too scared too ask help for that 🙂 .

now the java null pointer exception are on

target.x = f.position.x;  
target.y = f.position.y;

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 11, 2018, 12:54pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/27 "2018-09-11T12:54:00Z")

</div>

Yep sorry I forgot to edit that too:

```auto
target.x = food.x;
target.y = food.y;

```

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [September 11, 2018, 12:54pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/28 "2018-09-11T12:54:34Z")

</div>

maybe i did something wrong here is the code

```auto
float currentSmallest;
float dist;
for(int a = herb3f.size()-1; a >= 0; a--){
	Herbivor3F h3f = herb3f.get(a);
	if(h3f.faim() == false){
		h3f.randomMv();
	}
	else{
		PVector target = new PVector(0,0);
		currentSmallest = (parent.height * parent.height) + (parent.width * parent.width);
					
		for(Iterator<Food2> i = food2.iterator(); i.hasNext();){
			Food2 f = i.next();
			ArrayList<PVector> foodList = f.getFood2();
				
			for(int b = 0; b < foodList.size(); b++){
				PVector food = foodList.get(b);
				dist = ((h3f.position.x - food.x) * (h3f.position.x - food.x)) + ((h3f.position.y - food.y) * (h3f.position.y - food.y));
				if(dist < currentSmallest){
					currentSmallest = dist;
					target.x = f.position.x;
					target.y = f.position.y;
				}
			}
		}
		h3f.findFood(target);
	}
}

```

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [September 11, 2018, 12:55pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/29 "2018-09-11T12:55:04Z")

</div>

yeah i should see that 🙂

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [September 11, 2018, 12:58pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/30 "2018-09-11T12:58:34Z")

</div>

its work !!!

wow they run to the food like fly on honey 😃

i dont know how to thank you enough !

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 11, 2018, 1:00pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/31 "2018-09-11T13:00:24Z")

</div>

Hapy to hear that 🙂

Now try to really understand what I did and why your solution was not working.

---

<div class="post-metadata">

**Author:** ![vincentstrebelle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vincentstrebelle/32/6963_2.png) [@vincentstrebelle](https://discourse.processing.org/u/vincentstrebelle)\
**Post date:** [September 11, 2018, 1:01pm UTC](https://discourse.processing.org/t/problem-with-a-loop/3415/32 "2018-09-11T13:01:32Z")

</div>

yes thats my homework for today i guess 🙂

[Previous page](https://discourse.processing.org/t/problem-with-a-loop/3415.md?page=1)
