我目前正在为一个学校项目工作,并且正在观看视频教程,我对编码还很陌生。据我所知,一切看起来都不错,但是当我运行预览时,它向我发送了一个空白窗口,错误为“ 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);
最佳答案
在程序中使用数据之前,应养成检查数据是否存在的习惯:
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);
}
}