This site requires JavaScript, please enable it in your browser!
Greenfoot back
CavemanWrath
CavemanWrath wrote ...

2017/12/16

ArrayList error

CavemanWrath CavemanWrath

2017/12/16

#
I have created a program that in theory should create a list based on the objects around it, and call a variable from those objects, which would then change the image of those depending on their value. The problem is that I am getting two types of errors that do not make sense to me: java.lang.IndexOutOfBoundsException: Index: 8, Size: 8 as well as java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. I cannot for the life of me find out what the first error even means, and what the second error is referring to, as everything that I am doing seems to be in an order that would allow for the calling of the location would work. Here is the code for the Gifts Class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import greenfoot.*;
import java.util.*;
public class Gifts extends Actor
{
    static int amountTouching; //counts how many teachers it is touching
    int touchingZero; //looks for the adjacent gifts that are not next to a bomb
    List<Gifts> gifts = new ArrayList<Gifts> ();
    /**
     * Act - do whatever the Gifts wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            amountTouching = getNeighbours(51, true, Teacher.class).size();
            checkProximity();
        }
    }   
    public void checkProximity()
    {
        if (amountTouching == 0)
        {
            setImage("Hill.png");
            checkGifts();
        }
        else
        {
            setImage(amountTouching + "adjacent.png");
        }
    }
    public void checkGifts()
    {
        gifts = getNeighbours(51, true, Gifts.class);
        int i = 8;
        int checkNumber = 8;
        while (i > 0)
        {  
            if (gifts.get(i).amountTouching == 0)
            {
                gifts.get(i).setImage("empty.png");
            }
            i--;
        }
    }
}
Any and all help is greatly appreciated as I think I am a bit out of my depth here. Thanks!
Super_Hippo Super_Hippo

2017/12/16

#
The first element in a list has the index 0, the last one has index size-1. So if the size is 8, the elements have the indices 0, 1, 2, 3, 4, 5, 6, 7. There is no 8. That's why you get the first error. I don't think that you want to have 'amountTouching' static. It makes it a class variable, so all gift objects share the same variable. Line 39 would be pretty useless with a static variable. Where are you getting the second error?
CavemanWrath CavemanWrath

2017/12/17

#
I don't exactly remember but whatever I did fixed it. It is very possible that I will run across some more bugs with it in the future though.
You need to login to post a reply.