We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › How to debug an InvocationTargetException
Page Index Toggle Pages: 1
How to debug an InvocationTargetException? (Read 1168 times)
How to debug an InvocationTargetException?
Nov 19th, 2009, 10:45am
 
I'm trying to call the following method of my Asteroid class:

Quote:
void explode() {
   // some nasty magic numbers in here...
   if(mass >= 3) {
     mass /= 6;
     w *= 0.75;
     h *= 0.75;
     vx = random(2)-1;
     vy = random(2)-1;
     float vx1 = -vx + (random(0.4)-0.2);
     float vy1 = -vy + (random(0.4)-0.2);
     space.asteroids.add(new Asteroid(x,y,w,h,mass,vx1,vy1,space));
   }
   else {
     removeAsteroid();

     if(space.asteroids.size() == 0){
       println("level complete");
     }

   }
 }


...but the highlighted line is causing it to throw the following error:

Quote:
java.lang.reflect.InvocationTargetException
     at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:723)
     at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:716)
     at processing.core.PApplet.handleDraw(PApplet.java:1439)
     at processing.core.PApplet.run(PApplet.java:1327)
     at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
     at java.util.ArrayList.RangeCheck(ArrayList.java:547)
     at java.util.ArrayList.get(ArrayList.java:322)
     at asteroids02$Space.draw(asteroids02.java:277)
     ... 8 more


Either I'm trying to do something 'the wrong way'; or I've got some trivial bug...  But I'm not too sure how to debug it.  If I've understood correctly the problem occurs when it tries to instantiate the new Object, but rather than showing that error this error is triggered to 'catch' it.  The weird thing is everything carries on running as it should...

Can I use try/catch in some way to display the invocation error?  
Re: How to debug an InvocationTargetException?
Reply #1 - Nov 19th, 2009, 1:30pm
 
You have to read the full stack trace... Smiley
The real error is:
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
Not sure how to initialized your array list to get this error...
Re: How to debug an InvocationTargetException?
Reply #2 - Nov 19th, 2009, 3:03pm
 
Thanks - that certainly helps give me a vague idea of what's causing the problem (and makes me feel a little foolish!)...  Could it be because I'm changing the size of the array from within a method that iterates over the same array every frame?  This is in the 'space' object's draw loop:

Code:
for(int i=0; i<bullets.size(); i++) {
       for(int j=0; j<asteroids.size(); j++) {
        // <snip />
           thisAsteroid.explode();
   }
}


At this point my brain is trying to figure out if this means whether, since a new asteroid is added to the array, the loop is then trying to access the asteroid in the array before it's properly instantiated.  Would the instantiation of an object occur at the end of draw(), but the length returned by size() be updated before?  I think at least I'm nearer to understanding the cause of the problem...

But it's late here and I'm sure some fresh eyes (in the morning when I should be doing work) will resolve this, or find a better way to achieve the same result.

Obviously any further tips will be much appreciated Wink
Page Index Toggle Pages: 1