Pregunta

Tengo un problema pequeño que obtuve Error java.lang.indexoutofboundsexception: Índice: 29, tamaño: 29 Cuando inicie este error de código está en la línea if ((listaSwiat != null && listaSwiat.get(x) != null) || harm.get(y).getDzienTygodnia(x + 1).equals("Nd")), pero no dunno, ¿por qué el índice no debe ser de 30 NO1, puede ayudar?

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);
                }

¿Fue útil?

Solución

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.

Otros consejos

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);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top