学校项目,数组索引超出界限例外:2

时间:2015-10-25 21:39:04

标签: java processing indexoutofboundsexception

我目前正在为一所学校项目工作,并且正在关注视频教程,我对编码很陌生。从我可以告诉一切看起来正确,但是当我运行预览时,它向我发送一个空白窗口,其中包含错误" ArrayIndexOutOfBoundsException:2"

PShape baseMap;
String csv[];
String myData[][];

//Setup BaseMap and csv info

void setup() {
 size(1800, 900);
 noLoop();
 baseMap = loadShape("WorldMap.svg");
 csv = loadStrings("FlightCancellations.csv");
 myData = new String[csv.length][4];
 for(int i=0; i<csv.length; i++) {
 myData[i] = csv[i].split(",");

  } 
}


//draw
void draw() {
  shape(baseMap, 0, 0, width, height);
  noStroke();
  fill(255, 0, 0, 50);

  for(int i=0; i<myData.length; i++){


    float graphLong = map(float(myData[i][2]), -180, 180, 0, width);
   float graphLat = map(float(myData[i][3]), -90, 90, 0, height);

          println(graphLong + " / " + graphLat);

   ellipse(graphLong, graphLat, 10, 10);

  }
}

此外,映射图像工作正常,直到我添加

for(int i=0; i<myData.length; i++){


    float graphLong = map(float(myData[i][2]), -180, 180, 0, width);
   float graphLat = map(float(myData[i][3]), -90, 90, 0, height);

          println(graphLong + " / " + graphLat);

1 个答案:

答案 0 :(得分:1)

在您的程序中使用数据之前,您应养成检查数据的习惯:

for(int i=0; i<myData.length; i++) {
    if (myData[i].length > 3) { // Check that the array has at least 4 entries
        float graphLong = map(float(myData[i][2]), -180, 180, 0, width);
        float graphLat = map(float(myData[i][3]), -90, 90, 0, height);
        println(graphLong + " / " + graphLat);
        ellipse(graphLong, graphLat, 10, 10);
    }
}