IndirizzoOFBoundException Perché?
-
10-12-2019 - |
Domanda
Ho avuto un piccolo problema Ho ottenuto errori java.lang.indexoutofboundsexception: Indice: 29, Dimensione: 29 Quando avvio questo errore di codice è in linea if ((listaSwiat != null && listaSwiat.get(x) != null) || harm.get(y).getDzienTygodnia(x + 1).equals("Nd"))
ma non dunno perché indice dovrebbe essere 30 non 29 può aiutare?
Soluzione
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.
Altri suggerimenti
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);
}