Java - Error ArrayIndexOutOfBoundsExceptions: 11

 
Vista:

Error ArrayIndexOutOfBoundsExceptions: 11

Publicado por Jordi (4 intervenciones) el 21/05/2013 11:43:55
Hola! Estoy usando un programa que se llama Processing que usa programación java y tengo un problema y no se solventarlo. Al compilarlo el programa principal funciona pero la clase tabla que he creado no.

El error que sale es:

ArrayIndexOutOfBoundsExceptions: 11

El codigo es este:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class Table {
  int rowCount;
  String[][] data;
 
 
  Table(String filename) {
    String[] rows = loadStrings(filename);
    data = new String[rows.length][];
 
    for (int i = 0; i < rows.length; i++) {
      if (trim(rows[i]).length() == 0) {
        continue; // skip empty rows
      }
      if (rows[i].startsWith("#")) {
        continue;  // skip comment lines
      }
 
      // split the row on the tabs
      String[] pieces = split(rows[i], TAB);
      // copy to the table array
      data[rowCount] = pieces;
      rowCount++;
 
      // this could be done in one fell swoop via:
      //data[rowCount++] = split(rows[i], TAB);
    }
    // resize the 'data' array as necessary
    data = (String[][]) subset(data, 0, rowCount);
  }
 
 
  int getRowCount() {
    return rowCount;
  }
 
 
  // find a row by its name, returns -1 if no row found
  int getRowIndex(String name) {
    for (int i = 0; i < rowCount; i++) {
      if (data[i][0].equals(name)) {
        return i;
      }
    }
    println("No row named '" + name + "' was found");
    return -1;
  }
 
 
  String getRowName(int row) {
    return getString(row, 0);
  }
 
 
 String getString(int rowIndex, int column) {
   return data[rowIndex][column];  // Aqui es donde sale el error
 }
 
 
 String getString(String rowName, int column) {
  return getString(getRowIndex(rowName), column);
 }
 
 
  int getInt(String rowName, int column) {
    return parseInt(getString(rowName, column));
  }
 
 
  int getInt(int rowIndex, int column) {
    return parseInt(getString(rowIndex, column));
  }
 
 
  float getFloat(String rowName, int column) {
    return parseFloat(getString(rowName, column));
  }
 
 
  float getFloat(int rowIndex, int column) {
    return parseFloat(getString(rowIndex, column));
  }
 
 
  void setRowName(int row, String what) {
    data[row][0] = what;
  }
 
 
  void setString(int rowIndex, int column, String what) {
    data[rowIndex][column] = what;
  }
 
 
  void setString(String rowName, int column, String what) {
    int rowIndex = getRowIndex(rowName);
    data[rowIndex][column] = what;
  }
 
 
  void setInt(int rowIndex, int column, int what) {
    data[rowIndex][column] = str(what);
  }
 
 
  void setInt(String rowName, int column, int what) {
    int rowIndex = getRowIndex(rowName);
    data[rowIndex][column] = str(what);
  }
 
 
  void setFloat(int rowIndex, int column, float what) {
    data[rowIndex][column] = str(what);
  }
 
 
  void setFloat(String rowName, int column, float what) {
    int rowIndex = getRowIndex(rowName);
    data[rowIndex][column] = str(what);
  }
}


he indicado con un comentario donde sale el error...

Alguien puede ayudarme?

Gracias por leer el mensaje
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Error ArrayIndexOutOfBoundsExceptions: 11

Publicado por Jordi (4 intervenciones) el 21/05/2013 17:40:09
En otra versión del programa processing, en consola tambien me aparece esto:

Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 11
at Ex11_02$Table.getString(Ex11_02.java:136)
at Ex11_02$Table.getFloat(Ex11_02.java:161)
at Ex11_02.draw(Ex11_02.java:64)
at processing.core.PApplet.handleDraw(PApplet.java:2266)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2140)
at java.lang.Thread.run(Thread.java:662)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Jhonnathan Emilio Cardona Saineda

Error ArrayIndexOutOfBoundsExceptions: 11

Publicado por Jhonnathan Emilio Cardona Saineda (328 intervenciones) el 21/05/2013 19:09:37
La excepción es porque estas accediendo a una posicion del array que no existe. Por ejemplo si tu array es de tamaño 10 por obvias razones no existe la pasicion 10 ni 11 ni 12.. etc. si se trata de acceder de la manera array[12]=loQueSea; lanzará la excepción. No puedo ser mas concreto con tu error porque solo colocaste una parte de tu código así como solo una parte del error.
espero te sirva, saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Error ArrayIndexOutOfBoundsExceptions: 11

Publicado por Jordi (4 intervenciones) el 22/05/2013 10:32:46
Gracias por la respuesta entiendo lo que me quieres decir, pero sigo sin ver el problema...

Te adjunto el programa principal, la tabla y la base de datos que cargo para leerla.

Programa principal:

// Ex11_02

color[] minard = {#666666, #607F9C, #E9CCAE, #FFFFF3, #D01312};
color[] palette = minard;
PFont labelFont;

Table pacientes;
int rowCount;
int d = 10;

void setup() {
size(600, 500);
pacientes = new Table("pacientes.tsv");
rowCount = pacientes.getRowCount();
println("rowCount = " + rowCount);
labelFont = loadFont("GillSans-Bold-18.vlw");
smooth();
}

void draw() {
background(palette[0]);
textFont(labelFont);
stroke(180);
fill(180);

// Line and labels for X axis
textAlign(CENTER);
line(100, 400, 550, 400);
for (int i = 0; i < 7; i++) {
text (i-3, i * 75 + 100, 420);
}
text("SCORE2", 325, 445);

// Line and labels for Y axis
textAlign(RIGHT);
line(100, 50, 100, 400);
for (int i = 0; i < 8; i++) {
text (i-3, 95, 400-i*50);
}
text("TSQUARED", 70, 250);

// Gets data, draws dots
for (int row = 0; row < rowCount; row++) {
// State names
String paciente = pacientes.getString(row, 0);

// videoGames
float score2 = pacientes.getFloat(row, 4);
float x = map(score2, -3, 3, 100, 555);

// dance
float tsquared = pacientes.getFloat(row, 5);
float y = map(tsquared, -3, 4, 400, 50);

noStroke();
fill(#FFBA00, 180);
ellipse(x, y, d, d);

textAlign(LEFT);
fill(180);
if(dist(x, y, mouseX, mouseY) < (d/2+1)) {
text(paciente, x, y - 10);
}
}
}

La tabla:

class Table {
int rowCount;
String[][] data;


Table(String filename) {
String[] rows = loadStrings(filename);
data = new String[rows.length][];

for (int i = 0; i < rows.length; i++) {
if (trim(rows[i]).length() == 0) {
continue; // skip empty rows
}
if (rows[i].startsWith("#")) {
continue; // skip comment lines
}

// split the row on the tabs
String[] pieces = split(rows[i], TAB);
// copy to the table array
data[rowCount] = pieces;
rowCount++;

// this could be done in one fell swoop via:
//data[rowCount++] = split(rows[i], TAB);
}
// resize the 'data' array as necessary
data = (String[][]) subset(data, 0, rowCount);
}


int getRowCount() {
return rowCount;
}


// find a row by its name, returns -1 if no row found
int getRowIndex(String name) {
for (int i = 0; i < rowCount; i++) {
if (data[i][0].equals(name)) {
return i;
}
}
println("No row named '" + name + "' was found");
return -1;
}


String getRowName(int row) {
return getString(row, 0);
}


String getString(int rowIndex, int column) {
return data[rowIndex][column];
}


String getString(String rowName, int column) {
return getString(getRowIndex(rowName), column);
}


int getInt(String rowName, int column) {
return parseInt(getString(rowName, column));
}


int getInt(int rowIndex, int column) {
return parseInt(getString(rowIndex, column));
}


float getFloat(String rowName, int column) {
return parseFloat(getString(rowName, column));
}


float getFloat(int rowIndex, int column) {
return parseFloat(getString(rowIndex, column));
}


void setRowName(int row, String what) {
data[row][0] = what;
}


void setString(int rowIndex, int column, String what) {
data[rowIndex][column] = what;
}


void setString(String rowName, int column, String what) {
int rowIndex = getRowIndex(rowName);
data[rowIndex][column] = what;
}


void setInt(int rowIndex, int column, int what) {
data[rowIndex][column] = str(what);
}


void setInt(String rowName, int column, int what) {
int rowIndex = getRowIndex(rowName);
data[rowIndex][column] = str(what);
}


void setFloat(int rowIndex, int column, float what) {
data[rowIndex][column] = str(what);
}


void setFloat(String rowName, int column, float what) {
int rowIndex = getRowIndex(rowName);
data[rowIndex][column] = str(what);
}
}

La base de datos:

Paciente1 22,3 37,0 -0,76 0,24 -0,78 -1,19 -1,43 -0,98 -1,11 -0,76
Paciente2 25,5 33,4 -0,81 0,86 -0,79 -0,91 -0,69 -1,04 -1,10 -1,19
Paciente3 28,0 34,1 -0,10 -0,28 0,01 -0,37 0,17 1,18 0,16 -0,18
Paciente4 18,8 36,6 0,80 1,39 -1,39 -0,91 -1,12 -0,88 -1,07 -0,78
Paciente5 31,7 34,2 -1,41 1,04 -0,73 0,69 -1,41 1,05 -0,21 -0,50


Lo que yo pretendo es que cargue la base de datos, y represente en una gráfica las columnas 4 en el eje x y la columna 5 en el eje y. Y que sobre la gráfica al poner encima el cursor, aparezca que paciente es.

Haber si así puedes decirme en que me estoy equivocando.

Gracias por leer el mensaje y por tu ayuda.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Jhonnathan Emilio Cardona Saineda

Error ArrayIndexOutOfBoundsExceptions: 11

Publicado por Jhonnathan Emilio Cardona Saineda (328 intervenciones) el 23/05/2013 23:17:38
Lo que pasa es que para simular tu error necesitaria todo el codigo java.
Por ejemplo, donde esta el metodo loadStrings, o trim, o subset o printLn ????????
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar