IndexOutOfBoundExceptionなぜですか?
-
10-12-2019 - |
質問
私はjava.lang.indexoutofboundSexception:インデックス:29、サイズ:29私がこの1コードエラーを起動すると、if ((listaSwiat != null && listaSwiat.get(x) != null) || harm.get(y).getDzienTygodnia(x + 1).equals("Nd"))
にありますが、DunnoはなぜIndex 29 Any1は役に立ちませんか?
for (int y = 0; y < harm.size(); y++) {//wiersze
c1 = new PdfPCell(new Phrase(harm.get(y).nazwa, stdFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("" + harm.get(y).getSumaGodzin() + " / " + harm.get(y).normaGodzin, smallFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
for (int x = 0; x < harm.get(y).dni.size(); x++) {//kolumny
c1 = new PdfPCell(new Phrase(harm.get(y).dni.get(x).godziny, smallFont));
//dla swiąt ustal kolor tła na czerwono
//dla niedziel ustala kolor tla na czerwony
if ((listaSwiat != null && listaSwiat.get(x) != null) || harm.get(y).getDzienTygodnia(x + 1).equals("Nd")) {
c1.setBackgroundColor(BaseColor.RED);
}
. 解決
It looks like you are looping over all the elements of harm.get(y).dni and inside the loop you do
if ((listaSwiat != null && listaSwiat.get(x) != null)
|| harm.get(y).getDzienTygodnia(x + 1).equals("Nd"))
The last time through the loop x = 28 and the size is 29. But you do
harm.get(y).getDzienTygodnia(x + 1)
So you get the element at spot 29 which is out of bounds, because like the other answer stated the index starts at 0 not 1. You have to add a check here to see if you are currently at the last index before checking the next index.
他のヒント
In Java (and many other programming languages), indices start at zero, not one.
This means that, if the size is 29, the last valid index is 28, not 29.
Break apart the line that causes the exception so you can see exactly which call to get
is failing.
boolean listaSwiatCheck = listaSwiat != null && listaSwiat.get(x) != null;
if (listaSwiatCheck || harm.get(y).getDzienTygodnia(x + 1).equals("Nd")) {
c1.setBackgroundColor(BaseColor.RED);
}