views:

118

answers:

4

I seem to be getting an array out of bounds exception but the problem is the error message and my System.out and eclipse's debug tools tell me conflicting information.

This is my exception:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 9, Size: 9
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at InduceC45.C45(InduceC45.java:61)
at InduceC45.main(InduceC45.java:15)

Line 61 in InduceC45 is:

for(int attLoop = 1; attLoop <= attributes.get(splitAtt); attLoop++){

It's saying that splitAtt is out of bounds for ArrayList attributes, simple enough.

So I tried using the debugger and I also tried putting some System.out's to try to figure out what was going on, they basically gave me the same info, so here is the System.out's

System.out.println("splitAtt="+splitAtt);
System.out.println("attributes="+attributes);
System.out.println("attributes="+attributes.get(splitAtt));

This the print outs resulted in this:

splitAtt=9
attributes=[3, 3, 3, 2, 3, 6, 3, 4, 4, 2]
attributes=2

I really don't understand why I can reference it and not get an error if it's not in the loop. I don't think I'm doing anything tricky, let me know if anyone can think of a possible cause.

In case anyone wants the full source... http://github.com/Ryuho/CSC466/blob/master/lab3/src/InduceC45.java Line 61

A: 

Doesn't the first arrays item start from 0?

for(int attLoop = 0; attLoop < attributes.get(splitAtt); attLoop++){

I've been away from java for some time, I might be wrong.

Christian Sciberras
I was able to reference attributes.get(9) by it's self, i.e- System.out.println("attributes="+attributes.get(splitAtt));, but it somehow throws and exception if it's in a for declaration.
Ryuho
I changed the <= to < just to make sure, it still throws an exception.
Ryuho
Why did I get a -1???
Christian Sciberras
-1: Your answer isn't right. You might want to remove it. You're right about java's arrays being 0-indexed, but the cause of exception is "attributes.get(splitAtt)", and splitAtt isn't assigned in this loop. Fixing the loop's index doesn't affect the cause of the exception at all.
Iravanchi
This answer will stay here, just as he is still saying (in the question) that he is accessing the array wrongly. My answer might be irrelevant, but not wrong. If you please remove the unfair negative score and use your awesome weapon on really bad answers?
Christian Sciberras
+3  A: 

Inside of the loop, you remove an item from the list:

attributes.remove(splitAtt); // Line 75

On the second go around, the call to attributes.get(splitAtt) will then reference an index no longer in the list. Since you haven't removed that last element before you enter the loop, your print statements succeed.

Tim Stone
This is it. The error isn't happening the first time it gets to line 61, but the second.
ColinD
You are correct, thanks for catching that.
Ryuho
+4  A: 

I saw the whole code. You are removing elements from attributes using attributes.remove(splitAtt) as you go on like this the for condition causes the Exception when the size of the list is less.

Faisal Feroz
You are correct. Thanks for the reply.
Ryuho
+2  A: 
  1. Your print outs work because they are before the loop, and the exception occurs not on the first iteration of the loop, but on the next ones.

  2. You modify the "attributes" inside the loop, removing elements from it. On the next iteration, when it wants to check the loop condition, it fails.

Maybe you need to store attributes.get(splitAtt) in a variable before the loop, so that you can avoid accessing attributes on the loop condition.

And if you want to see exactly what's happening using the system outs, try putting them at the end of the loop too, so that you can see the values each time the loop conditions is going to be checked.

Iravanchi