Question

Alright so I remove an object from an array list, then break, but I still get OutOfBounds, I'm kinda confused, could someone help me, I've tried to isolate the problem, but I still can't figure it out.

Here is the error I get:

Exception in thread "Thread-2" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.get(ArrayList.java:411)
    at GameFunctions.closestTarget(GameFunctions.java:37)
    at GameFunctions.act(GameFunctions.java:147)
    at GamePanel$1.run(GamePanel.java:50)

Here are the two methods causing me problems:

public void act(ArrayList<Germs> g, ArrayList<WhiteBloodCell> p, String actor)
  {
    if(actor.equals("Germs"))
    { 
      for(int i=0;i<g.size();i++)
      {
        if(g.get(i) instanceof SPneumoniae)
        {
          g.get(i).move();
          g.get(i).testBounds();
          if(checkSize(g, i))
          {
            System.out.println("broken");
            break;
          }
        }
      }
    }
    else if(actor.equals("WhiteBloodCells"))
    {
      for(int i=0;i<p.size();i++)
      {
        p.get(i).setTarget(closestTarget(g, p.get(i)));
        p.get(i).move();
      }
    }
  }

And here is the method called that's removing the object:

public boolean checkSize(ArrayList<Germs> g, int i)
  {
    if(g.get(i).getRadius() > 30)
    {
      g.get(i).setRadius(30);
    }
    else if(g.get(i).getRadius() <= 0)
    {
      g.remove(i);
      return true;
    }

    return false;
  }
Was it helpful?

Solution

It looks like the error is due to there being nothing in the ArrayList of g.

Check out this area of your code:

else if(actor.equals("WhiteBloodCells"))
{
  for(int i=0;i<p.size();i++)
  {
    p.get(i).setTarget(closestTarget(g, p.get(i)));
    p.get(i).move();
  }
}

See if that gives you any leads.

Edit -- The bug IS coming from the closestTarget function based on the exceptions listed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top