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

2019/2/20

java.lang.IndexOutOfBoundsException

Zestix Zestix

2019/2/20

#
Please help, I was trying to start my game once I click on a specific Object but it seems like it doesn't work.. This is my code for the world the necessary coding is in public void act():
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class PlantsVsZombies extends World
{
    int Points = 200;
    int spawntime1 = 800+ Greenfoot.getRandomNumber(601);
    int spawntime2 = 800+ Greenfoot.getRandomNumber(602);
    int spawntime3 = 800+ Greenfoot.getRandomNumber(603);
    int spawntime4 = 800+ Greenfoot.getRandomNumber(604);
    int spawntime5 = 800+ Greenfoot.getRandomNumber(605);
    static boolean spawn = false;
    public static boolean getSpawn()
    {
        return spawn;
    }
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public PlantsVsZombies()
    {
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1400, 600,1);
        setBackground("BackGround.jpg");
        prepare();
    }

    public void act()
    {
        showText(""+spawntime1,700,300);
        Preparation prep = getObjects(Preparation.class).get(0);
        if (!getObjects(Preparation.class).isEmpty()
        && Greenfoot.mousePressed(prep))
        {
            spawn = true;
        }
        if (spawn == true)
        {
            spawntime1--;
            spawntime2--;
            spawntime3--;
            spawntime4--;
            spawntime5--;
        }
        spawnlane1();
        spawnlane2();
        spawnlane3();
        spawnlane4();
        spawnlane5();
        
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare(){

        Bar bar = new Bar();
        addObject(bar,94,302);
        bar.setLocation(64,238);
        bar.setLocation(91,300);
        bar.setLocation(91,300);
        Mower mower = new Mower();
        addObject(mower,270,136);
        mower.setLocation(214,143);
        Mower mower2 = new Mower();
        addObject(mower2,214,217);
        Mower mower3 = new Mower();
        addObject(mower3,214,315);
        Mower mower4 = new Mower();
        addObject(mower4,214,403);
        Mower mower5 = new Mower();
        addObject(mower5,214,506);
        mower5.setLocation(212,512);
        mower4.setLocation(215,410);

        Green green = new Green();
        addObject(green,117,127);

        Sun sun = new Sun();
        addObject(sun,115,234);

        Points points = new Points();
        addObject(points,58,40);
        showText("=  "+ Points,120,40);
        Preparation preparation = new Preparation();
        addObject(preparation,1255,49);
    }

    private void spawnlane1()
    {
        if (spawntime1 < 0)
        {
            addObject(new Zombie(),1400,135);
            spawntime1 = 800+ Greenfoot.getRandomNumber(600);
        }
    }

    private void spawnlane2()
    {
        if (spawntime2 < 0)
        {
            addObject(new Zombie(),1400,217);
            spawntime2 = 800+ Greenfoot.getRandomNumber(600);
        } 
    }

    private void spawnlane3()
    {
        if (spawntime3 < 0)
        {
            addObject(new Zombie(),1400,315);
            spawntime3 = 800+ Greenfoot.getRandomNumber(600);
        } 
    }

    private void spawnlane4()
    {
        if (spawntime4 < 0)
        {
            addObject(new Zombie(),1400,410);
            spawntime4 = 800+ Greenfoot.getRandomNumber(600);
        } 
    }

    private void spawnlane5()
    {
        if (spawntime5 < 0)
        {
            addObject(new Zombie(),1400,512);
            spawntime5 = 800+ Greenfoot.getRandomNumber(600);
        } 
    }
}
The error message looks like this:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at PlantsVsZombies.act(PlantsVsZombies.java:37)
	at greenfoot.core.Simulation.actWorld(Simulation.java:573)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at PlantsVsZombies.act(PlantsVsZombies.java:37)
	at greenfoot.core.Simulation.actWorld(Simulation.java:573)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at PlantsVsZombies.act(PlantsVsZombies.java:37)
	at greenfoot.core.Simulation.actWorld(Simulation.java:573)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at PlantsVsZombies.act(PlantsVsZombies.java:37)
	at greenfoot.core.Simulation.actWorld(Simulation.java:573)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at PlantsVsZombies.act(PlantsVsZombies.java:37)
	at greenfoot.core.Simulation.actWorld(Simulation.java:573)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at PlantsVsZombies.act(PlantsVsZombies.java:37)
	at greenfoot.core.Simulation.actWorld(Simulation.java:573)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at PlantsVsZombies.act(PlantsVsZombies.java:37)
	at greenfoot.core.Simulation.actWorld(Simulation.java:573)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.NullPointerException
	at Preparation.act(Preparation.java:24)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.NullPointerException
	at Preparation.act(Preparation.java:25)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.NullPointerException
	at Preparation.act(Preparation.java:25)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.NullPointerException
	at Preparation.act(Preparation.java:27)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.NullPointerException
	at Preparation.act(Preparation.java:27)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at PlantsVsZombies.act(PlantsVsZombies.java:37)
	at greenfoot.core.Simulation.actWorld(Simulation.java:573)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:506)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
Greetings
danpost danpost

2019/2/20

#
Maybe I was not precise enough in your other discussion thread about this issue. To spell it out: (1) the World's getObjects method returns a List object; (2) the List's get method returns an element from the list; (3) the IndexOutOfBoundsException is thrown if you try to get an element from a list that does not exist; You must ensure that the list contains the element you are trying to get from the list. The code above does not do this. It tries to get element 0 from the list at line 31 without first making sure that the element exists. Line 32 actually performs that check, but too late. Apparently, there is a problem in your Preparation class. Posts its code, in its entirety, here for review.
Zestix Zestix

2019/2/21

#
Thats the code of the other class:
public class Preparation extends Actor
{
    /**
     * Act - do whatever the Preparation wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mousePressed(this))
        {
            getWorld().removeObject(this);
        }
    }    
}
danpost danpost

2019/2/21

#
Zestix wrote...
Thats the code of the other class: << Code Omitted >>
No problem there, now. You must have already corrected it.
Zestix Zestix

2019/2/21

#
danpost wrote...
Zestix wrote...
Thats the code of the other class: << Code Omitted >>
No problem there, now. You must have already corrected it.
It doesn’t work though
danpost danpost

2019/2/21

#
Zestix wrote...
<< Quote Omitted >> It doesn’t work though
Change lines 32-33 to:
if (getObjects(Preparation.class).isEmpty())
The Preparation object removes itself when clicked on. The world only needs to see that is has been removed.
Zestix Zestix

2019/2/21

#
danpost wrote...
Zestix wrote...
<< Quote Omitted >> It doesn’t work though
Change lines 32-33 to:
if (getObjects(Preparation.class).isEmpty())
The Preparation object removes itself when clicked on. The world only needs to see that is has been removed.
Thank you very much
You need to login to post a reply.