THE CODE BELOW WILL SOLVE THE PROBLEM OF Your program crashing when a fish is eaten while it is not the current object in use

***************SCROLL DOWN TO SEE USAGE OF THE instanceof OPERATOR****************

add the code below to your Minnow
notice the beginning of the ACT method

    /** Checks whether this fish is in an environment.
* @return <code>true</code> if the fish is in the environment
* (and at the correct location); <code>false</code> otherwise
**/
public boolean isInEnv()
{
return environment().objectAt(location()) == this;
} // YOU ALREADY HAVE AN ACT METHOD... just add that top check to make sure
// object is in environment
/** Acts for one step in the simulation. **/ public void act() { // Make sure fish is alive and well in the environment -- fish // that have been removed from the environment shouldn't act. if ( ! isInEnv() ) return; // Try to breed. if ( ! breed() ) // Did not breed, so try to move. move(); // Determine whether this fish will die in this timestep. Random randNumGen = RandNumGenerator.getInstance(); if ( randNumGen.nextDouble() < probOfDying ) die(); } //---------------------------------------------------------------------------------- // USES THE "instanceof" OPERATOR protected boolean eat()
{
// Get list of neighboring locations.
ArrayList nbrs = environment().neighborsOf(location());
for ( int index = 0; index < nbrs.size(); index++ )
{
Location loc = (Location) nbrs.get(index);
if (! environment().isEmpty(loc) )
{
Minnow prey = (Minnow)environment().objectAt(loc);
if (! (prey instanceof HungryFish))
{
prey.die();
changeLocation(loc);
itsReserve +=5;
return true;
}
}
}
return false;
}