arrayIndexOutOfBoundsException:1
in
Programming Questions
•
2 years ago
Can anyone help me to understand why I get this exception.?
I modified Fry's Visualizing Data example about maps with single rollovers.
Here is the main code exctly the same of fry's:
- PImage mapImage;
- Table locationTable;
- Table nameTable;
- int rowCount;
- Table dataTable;
- float dataMin = MAX_FLOAT;
- float dataMax = MIN_FLOAT;
- void setup() {
- size(640, 500);
- mapImage = loadImage("mappa.png");
- locationTable = new Table("locations.tsv");
- rowCount = locationTable.getRowCount();
- // Read the data table
- dataTable = new Table("random.tsv");
- nameTable= new Table("names.tsv");
- // Find the minimum and maximum values
- for (int row = 0; row < rowCount; row++) {
- float value = dataTable.getFloat(row, 1);
- if (value > dataMax) {
- dataMax = value;
- }
- if (value < dataMin) {
- dataMin = value;
- }
- }
- PFont font = loadFont("Helvetica-Bold-12.vlw");
- textFont(font);
- smooth();
- noStroke();
- }
- float closestDist;
- String closestText;
- float closestTextX;
- float closestTextY;
- void draw() {
- background(255);
- image(mapImage, 0, 0);
- closestDist = MAX_FLOAT;
- for (int row = 0; row < rowCount; row++) {
- String abbrev = dataTable.getRowName(row);
- float x = locationTable.getFloat(abbrev, 1);
- float y = locationTable.getFloat(abbrev, 2);
- drawData(x, y, abbrev);
- }
- // Use global variables set in drawData()
- // to draw text related to closest circle.
- if (closestDist != MAX_FLOAT) {
- fill(0); textAlign(CENTER);
- text(closestText, closestTextX, closestTextY);
- }
- }
- void drawData(float x, float y, String abbrev) {
- float value = dataTable.getFloat(abbrev, 1);
- float radius = 0;
- if (value >= 0) {
- radius = map(value, 0, dataMax, 1.5, 15);
- fill(#333366); // blue
- } else {
- radius = map(value, 0, dataMin, 1.5, 15);
- fill(#ec5166); // red
- }
- ellipseMode(RADIUS);
- ellipse(x, y, radius, radius);
- float d = dist(x, y, mouseX, mouseY);
- // Because the following check is done each time a new
- // circle is drawn, we end up with the values of the
- // circle closest to the mouse.
- if ((d < radius + 2) && (d < closestDist)) {
- closestDist = d;
- String name = nameTable.getString(abbrev, 1);
- closestText = name + " " + value;
- closestTextX = x;
- closestTextY = y-radius-4;
- }
- }
- 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));
- }
- }
I'll post the tsv files as well.
locations.tsv
- AQ 439 270
- BR 94 325
- CH 148 241
- CI 368 247
- DR 56 176
- GI 220 183
- IS 376 120
- LE 256 166
- LU 110 331
- NI 278 267
- OC 232 380
- ON 143 101
- PA 405 168
- SE 437 165
- TA 357 147
- TO 302 194
- VA 453 203
random.tsv
- AQ 0.1
- BR -5.3
- CH 3
- CI 7
- DR 11
- GI 1.5
- IS -6.7
- LE -4
- LU 9
- NI -2.2
- OC -3.3
- ON 6.6
- PA 7.2
- SE 7.1
- TA 6.9
- TO 6
- VA 1.8
- AQ Aquila
- BR Bruco
- CH Chiocciola
- CI Civetta
- DR Drago
- GI Giraffa
- IS Istrice
- LE Leocorno
- LU Lupa
- NI Nicchio
- OC Oca
- ON Onda
- PA Pantera
- SE Selva
- TA Tartuca
- TO Torre
- VA Valdimontone
2