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

2019/3/16

Error:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

rma943 rma943

2019/3/16

#
I am working on a project for class where we have to create a game where a person avoids robots(Chasers) that explode when they hit each other and move closer to the runner when the runner moves. Every time that I try to run the program, I get a list of errors saying "java.lang.IndexOutOfBoundsException: Index: 0, Size: 0" on Chaser.java:38. Here is my code for the chaser class. Can someone please help me? Thank you!
 
    public void act()
    {
        this.move(1);
        //Remove chaser if chaser is touching explosion
        if(isTouching(Explosion.class)){
        getWorld().removeObject(this);
        }
        
        //remove chaser if chaser is touching another chaser
        if(isTouching(Chaser.class)){
        getWorld().addObject(new Explosion(),getX(),getY());
        removeTouching(Runner.class);
        //World().removeObject(Chaser);
        }
        
        //have chasers follow the runner if runner is still in the world
        Runner runner = (Runner)getWorld().getObjects(Runner.class).get(0);
        int RunnerX = runner.getX();
        int RunnerY = runner.getY();
        {
            turnTowards(RunnerX,RunnerY);
            move(1);
        }
       
    }    
}
danpost danpost

2019/3/16

#
Line 16 says:
if runner is still in the world
but you do not check that first.
rma943 rma943

2019/3/16

#
danpost wrote...
Line 16 says:
if runner is still in the world
but you do not check that first.
I thought that is what line 17 is doing?
danpost danpost

2019/3/16

#
rma943 wrote...
I thought that is what line 17 is doing?
You are mistaken. The getObjects method returns a list which may or may not be empty. If it is empty, the get method will fail, as you see.
rma943 rma943

2019/3/16

#
Okay. Thank you
rma943 rma943

2019/3/16

#
danpost wrote...
rma943 wrote...
I thought that is what line 17 is doing?
You are mistaken. The getObjects method returns a list which may or may not be empty. If it is empty, the get method will fail, as you see.
So how do I check if the runner is still in the world?
danpost danpost

2019/3/16

#
rma943 wrote...
So how do I check if the runner is still in the world?
Check if the list is empty.
Dr.Blythe Dr.Blythe

2019/3/19

#
Of course, since we didn't cover how to use the results of getObjects (or even mention the get() method), there must be another (much simpler) way to do this!
danpost danpost

2019/3/20

#
Dr.Blythe wrote...
Of course, since we didn't cover how to use the results of getObjects (or even mention the get() method), there must be another (much simpler) way to do this!
You could keep a reference to the Runner object in your World subclass along with a "getter" method:
private Runner runner;

// in constructor (or prepare method)
runner = new Runner();
addObject(runner, << wherever >>);

//
public Runner getRunner()
{
    return runner;
}
Then, use the following in your Actor subclass:
Actor runner = ((MyWorld)getWorld()).getRunner();
if (runner.getWorld() != null)
{
    ...
You need to login to post a reply.