I have this overflow problem in an array that I have within several objects.
java.lang.ArrayIndexOutOfBoundsException: 7 at Exercise.Visiting.run (Visitor.java:73)
The code
public class Visitor extends Thread {
private String salaReg[];
private String salaRegAnt[];
private Semaphore sem;
private String id;
private Room sala[];
public Visitante(String id, Room sala[], Semaphore sem) {
this.id = id;
this.sala = sala;
this.sem = sem;
}
@Override
public void run() {
boolean listo = true;
boolean existe = true;
int j = 0;
int k = 0;
int numRan;
try {
sem.acquire();
System.out.println("/////////////////HILO " + id);
System.out.println("Estoy en sala " + sala[0].getId());
salaReg = sala[0].getReg();
System.out.println("Actual");
for (int m = 0; m < salaReg.length; m++) {
System.out.printf(salaReg[m] + " | ");
}
while (listo) {
if (salaReg[j].equals(" ")) {
salaReg[j] = id;
sala[0].setReg(salaReg);
listo = false;
} else {
j++;
}
}
j = 0;
listo = true;
numRan = (int) (Math.random() * 2000 + 400);
Thread.sleep(numRan);
System.out.println("Museo con " + sala.length + " salas");
for (int i = 1; i < sala.length; i++) {
salaReg = null;
System.out.println("Soy " + id + " y estoy en sala " + sala[i].getId());
salaReg = sala[i].getReg();//Array que se supone que debería estar vacio
//ya que a cada sala del array de salas
//se le ha "seteado" un array de tipo String
//con tamaño 7
//e inicializada a " "
salaRegAnt = sala[i - 1].getReg();//este array del anterior objeto(por eso i-1)
//debería tener 1 sólo dato
System.out.println("Actual");
for (int m = 0; m < salaReg.length; m++) {
System.out.printf(salaReg[m] + " | ");
}
System.out.println();
while (listo) {
if (salaReg[j].equals(" ")) {
salaReg[j] = id;
sala[i].setReg(salaReg);
System.out.println("Antes");
for (int m = 0; m < salaRegAnt.length; m++) {
System.out.printf(salaRegAnt[m] + " | ");
}
System.out.println();
while (existe) {
if (salaRegAnt[k].equals(id)) {
salaRegAnt[k] = " ";
sala[i - 1].setReg(salaRegAnt);
System.out.println("Despues");
for (int m = 0; m < salaRegAnt.length; m++) {
System.out.printf(salaRegAnt[m] + " | ");
}
System.out.println();
existe = false;
} else {
k++;
}
}
listo = false;
} else {
j++;
}
}
j = 0;
k = 0;
listo = true;
numRan = (int) (Math.random() * 2000 + 800);
Thread.sleep(numRan);
}
sem.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I clarify that in a main class I created an array of String, and that array of String was assigned to an array of objects.
I hope to receive some clarification, since I can not find the fault.