ArrayIndexOutOfBoundsException


#1

Hello, I am following Ben Fry’s visualizing data and try to make a map based on a dataset provided by the book’s website. I followed the most part of his code however an error happens on the code"float x = float(pieces[x])" and it showed ArrayIndexOutOfBoundsException:1, I had no idea how to fix that right now, asking for help. Thank you.
Here is my code

int CODE = 0;
int X = 1;
int Y = 2;
int NAME = 3;

//total number of places
int totalCount;
//an array for Place
Place[] places;
//number of places loaded
int placeCount;

//min/max boundary of all points
float minX, maxX;
float minY, maxY;

//border of where the map should be drawn on screen
float mapX1, mapY1;
float mapX2, mapY2;


//? difference between public setup() and setup()
void setup(){
  
  readData();
//size the map
  size(720, 453,P3D);
  mapX1 =30;
  mapX2 = width - mapX1;
  mapY1 = 20;
  mapY2 = height - mapY2;
}

void readData(){
  //create a array for all the string
  //equal to number of places
  String[] lines = loadStrings("zips.tsv");
  //read the header line
  parseInfo(lines[0]);
  //put Place particle into array
  places = new Place[totalCount];
  for(int i=0; i<lines.length; i++){
    places[placeCount] = parsePlace(lines[i]);
    placeCount++;
  }
}


void parseInfo(String line){
  //delete # and space
  String infoString = line.substring(2);
  String[] infoPieces = split(infoString, ",");
  totalCount = int(infoPieces[0]);
  minX = float(infoPieces[1]);
  maxX = float(infoPieces[2]);
  minY = float(infoPieces[3]);
  maxY = float(infoPieces[4]);
}

//converts a single line of data file into a Place object
Place parsePlace(String line){
  //tsv file data was sepearate by tab
  String pieces[] = split(line, TAB);
  //first column is zip
  int zip = int(pieces[CODE]);
  //second column is X
  float x = float(pieces[X]);
  //third column is Y
  float y = float(pieces[Y]);
  //fourth column is name
  String name = pieces[NAME];
  
  return new Place(zip, name, x, y);
}


void draw(){
  background(255);
  noStroke();
  fill(0);
  for(int i = 0; i <placeCount; i++){
    places[i].draw();
  }
}

// function for remap coordinate
float TX(float x){
  return map(x, minX, maxX, mapX1, mapX2);
}

float TY(float y){
  return map(y, minY, maxY, mapY1, mapY2);
}



and a class for a single Place

class Place{
  int code;
  String name;
  float x;
  float y;

//place constructor
Place(int code, String name, float x, float y){
  this.code = code;
  this.name = name;
  this.x = x;
  this.y = y;
}

void draw(){
  float xx = TX(x);
  float yy = TY(y);
  ellipse(xx,yy, 3,3);
}
 
}

For the dataset I downloaded it from http://benfry.com/writing/zipdecode/zips.tsv
btw, I am trying to find a another resource or tutorial for data viz by processing / openFramework, I find the book from Ben is a little bit hard to follow since it includes many Java syntax in it. Appreciate if anyone can provide me some suggestions. Thank you.


#2

Hm. The code has bugs … do you have a link to the original code?


#3

There is a online version for this book
https://books.google.com/books?id=6jsVAiULQBgC&printsec=frontcover&source=gbs_ge_summary_r&cad=0#v=onepage&q&f=false

From p155-p157


#4

First
You call readData before defining size(), it’s a mistake , defining size is the first function called you have
to put on setup()

Second
In draw() , use

 for(int i = 0; i <places.length; i++){
places[i].draw(); }

instead of

    for(int i = 0; i <placeCount; i++){
places[i].draw(); }

Because if placeCount is not equal to places.length its gonna create errors

Third

Make sure the file “zips.tsv” exists in your path . Go to Sketch -> Show Sketchbook Folder -> data folder


#5

thank you so much for your help.