tags:

views:

60

answers:

2

Hi

I don't know why this is generating an ArrayOutOfBoundsException! It's likely I've overlooked something because I'm so tired but if someone could tell me what is wrong I'd appreciate it. Thanks.

// array declaration
public int CircleLeft[] = new int[mIPAWidth];

// this is my loop that generates the error:
for(px = 0; px <= mIPAWidth - 1; px++){
py =(int) m.sqrt(r^2 - (px-mIPAWidthHalf)^2) + mIPAHeightHalf;
Log.w(getClass().getName(), "mIPAWidth = " + mIPAWidth + ", index is: " + px);
CircleLeft[px] = py; }
+1  A: 

What is the type of px? If it's a double, then you could suffer from a mis-compare with the <=(comparing for equality with double is almost never correct). Try the canonical loop for(px = 0; px < mIPAWidth ; px++) instead or better yet, make the loop index an integer.

Also, what's the type of mIPAWidth (for the same reasons)?

Luther Blissett
+1... it's another good reason to declare a new variable for your for loop counter.
iandisme
I did in the initial stage of this loop use a seperate 'n' variable as the counter but I think the issue may reside in mIPAwidth also being dynamic as its value is reset when the surface changes and this loop is part of that method, configureGeometry...so I suppose if mIPAWidth was to change when the array has already been initialised, it would generate the outofbounds exception I am experiencing here!
Greenhouse Gases
@GG: This was suggested by iandisme, wasn't it? You should check his answer instead then (You can change the accepted answer IIRC).
Luther Blissett
+2  A: 

Along with Luther Blissett's answer...

Are you modifying mIPAWidth between when the array is instantiated and when you begin the for loop? I would change your for conditional to something like this:

for(int px = 0; px < CircleLeft.length; px++)

That way you know the type of px and you know you're referencing the actual length of the CircleLeft array. It may not fix your error, but it's still a good idea.

iandisme
Thanks for your input. I think I may be referring to a variable that has a value that can change at runtime making access to an index in an already predefined array possibly throw an exception. I will change this now!
Greenhouse Gases