دنبال کننده ها

۱۳۹۶ شهریور ۱۰, جمعه

arrays - Java strage index out of bounds error when looping

[ad_1]



I do not know why I am getting the following error as the index isn't out of bounds:



SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IndexOutOfBoundsException: Index: 25, Size: 24] with root cause
java.lang.IndexOutOfBoundsException: Index: 25, Size: 24


Here is my code, you can see I have added println statements to monitor this:



for (MeterReadingsPercentagesHolder holder : percentagesList) 

System.out.println(holder.getPeriod()+" "+holder.getKwh());


BigDecimal totalOfExistingMeterReadings = BigDecimal.valueOf(0);
System.out.println("size: "+percentagesList.size());
for (int l = 0; l < 48; l++)
MeterReadingHHR seeIfReadingExists = meterReadingsDAO
.getSingleMeterReadingForIcpChannelDateAndPeriod(missing.getDate(),
missing.getIcpChannel(), l + 1);


if (seeIfReadingExists != null)
System.out.println("Not Null and SEE IF READING EXISTS: "+l);
percentagesList.remove(l);
totalUsageForToday = totalUsageForToday.subtract(seeIfReadingExists.getKwh());
totalOfExistingMeterReadings = totalOfExistingMeterReadings
.add(seeIfReadingExists.getKwh());
else if (seeIfReadingExists == null)




The error happens at the following line:



 percentagesList.remove(l);


But the array is clearly in bounds... Here is what prints to the console.



1 0E-28
2 0E-28
3 0E-28
4 0E-28
5 0E-28
6 0E-28
7 0E-28
8 0E-28
9 0E-28
10 0E-28
11 0E-28
12 0E-28
13 0E-28
14 0E-28
15 0E-28
16 0.00008527710560
17 0.001072055099
18 0.01412351374
19 0.02758917547
20 0.03700214408
21 0.05316418684
22 0.07106831914
23 0.07659914885
24 0.07222159050
25 0.08420505488
26 0.09173380546
27 0.09242414394
28 0.07893411733
29 0.06539942173
30 0.07177490090
31 0.06319846011
32 0.04661409262
33 0.03400932363
34 0.01607676565
35 0.002704502624
36 0E-28
37 0E-28
38 0E-28
39 0E-28
40 0E-28
41 0E-28
42 0E-28
43 0E-28
44 0E-28
45 0E-28
46 0E-28
47 0E-28
48 0E-28
size: 48
Not Null and SEE IF READING EXISTS: 1
Not Null and SEE IF READING EXISTS: 2
Not Null and SEE IF READING EXISTS: 3
Not Null and SEE IF READING EXISTS: 4
Not Null and SEE IF READING EXISTS: 5
Not Null and SEE IF READING EXISTS: 6
Not Null and SEE IF READING EXISTS: 7
Not Null and SEE IF READING EXISTS: 8
Not Null and SEE IF READING EXISTS: 9
Not Null and SEE IF READING EXISTS: 10
Not Null and SEE IF READING EXISTS: 11
Not Null and SEE IF READING EXISTS: 12
Not Null and SEE IF READING EXISTS: 13
Not Null and SEE IF READING EXISTS: 14
Not Null and SEE IF READING EXISTS: 15
Not Null and SEE IF READING EXISTS: 16
Not Null and SEE IF READING EXISTS: 17
Not Null and SEE IF READING EXISTS: 18
Not Null and SEE IF READING EXISTS: 19
Not Null and SEE IF READING EXISTS: 20
Not Null and SEE IF READING EXISTS: 21
Not Null and SEE IF READING EXISTS: 22
Not Null and SEE IF READING EXISTS: 23
Not Null and SEE IF READING EXISTS: 24
Not Null and SEE IF READING EXISTS: 25
Sep 02, 2017 12:47:02 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IndexOutOfBoundsException: Index: 25, Size: 24] with root cause
java.lang.IndexOutOfBoundsException: Index: 25, Size: 24



[ad_2]

لینک منبع