处理ArrayIndexOutOfBoundsException

时间:2013-02-05 10:02:00

标签: loops switch-statement processing

我正在为我正在制作的具有三种状态的交互式图形编写一些代码。我使用switch语句在显示图像数组的状态之间移动。我上传了所有图片但是当我调用一个包含8个图像数组的状态时,我上传了一条错误消息:ArrayIndexOutOfBoundsException 8

即使我在草图中加载了正确数量的图像,我也似乎无法摆脱它。

以下是我的代码,我正在使用Processing 2.0b7处理Macbook Pro OSX

PImage[] teams;
int n = 24;
PImage[] teams2;
int m = 16;
PImage [] quarterFinalWinners;
int o = 7;
PImage bg;
float damping = 0.1;
PFont font;
String[] positions, prevPositions;
float x;
float y;

int afconState = 3;

/**----------------------------------------------------------------------------------------------------------------------------**/

public void setup() {
  size(600, 1600);
   bg = loadImage("football-pitch-hi.png");

  positions = new String[]{"18", "26", "32", "45", "58", "56", "59", "61", "66", "69", "71", "85", "98", "100", "116", "133"};
  prevPositions = new String[]{"14", "19", "25", "30", "34", "45", "52", "69", "71", "72", "87", "84", "89", "105", "107", "110"};
  int a = 16;

  font = loadFont("Calibri-Bold-48.vlw");
  textFont(font);

  frame.setResizable(true);

  teams = new PImage[n];
    for (int i = 0; i < teams.length; i++) {
      teams[i] = loadImage(i + ".png");
    }

  teams2 = new PImage[m];
    for (int i = 0; i < teams2.length; i++) {
      teams2[i] = loadImage(i + ".gif");
    }

   quarterFinalWinners = new PImage[o];
    for (int o = 0; o < quarterFinalWinners.length; o++); {
   quarterFinalWinners[o] = loadImage(o + "_.png");
   }
}

/**----------------------------------------------------------------------------------------------------------------------------**/

void draw() {
  background(#000000);
  if ((x < width) && (y < height)) {
for (int i = 0; i < 16; i++);
    }


  }

/**----------------------------------------------------------------------------------------------------------------------------**/

public void keyPressed() {
  noLoop();

  switch(afconState) {
  case 1:
   afconState = 1;
   background(#000000);
  for (int i = 0; i < 16; i++) {
  image(teams[i], 150, 60*i);
  }
  }

  if ((key == 's') || (key == 'S')) {

    afconState = 2;
    background(#000000);
  for (int i = 0; i < 16; i++) {
  image(teams2[i], 150, 60*i);
  }
  }

  else if ((key == 'q') || (key == 'Q')) {
    afconState = 3;
    background(#000000);
  for (int i = 0; i < 16; i++) {
  image(quarterFinalWinners[i], 150, 60*i);
  }
  }
  break;

/**----------------------------------------------------------------------------------------------------------------------------**/    
    case 2:
    if((key == 's') || (key == 'S')) {

    afconState = 2;
  background(#000000);
  for (int i = 0; i < 16; i++) {
  image(teams2[i], 150, 60*i);
  }
  }

  else if ((key == 'q') || (key == 'Q')) {
    afconState = 3;
  for (int i = 0; i < 16; i++) {
  image(quarterFinalWinners[i], 150, 60*i);
  }
  }

  break;

  case 3:
  background(#000000);
  if ((key == 'q') || (key == 'Q')) {
    afconState = 3;
  for (int i = 0; i < 16; i++) {
  image(quarterFinalWinners[i], 150, 60*i);
  }
  }

  else if((key == 's') || (key == 'S')) {

    afconState = 2;
  background(#000000);
  for (int i = 0; i < 16; i++) {
  image(teams2[i], 150, 60*i);
  }
  }

  break;

  default:
  afconState = 1;
   background(#000000);
  for (int i = 0; i < 16; i++) {
  image(teams[i], 150, 60*i);
  }  

  if ((key == 's') || (key == 'S')) {
    afconState = 2;
  // modify the positions list
  for (int i = 0; i < 16; i++) {
  image(teams2[i], 150, 60*i);
  }
  }

  else if ((key == 'q') || (key == 'Q')) {
    afconState = 3;
  for (int i = 0; i < 16; i++) {
  image(quarterFinalWinners[i], 150, 60*i);
  }
  }

  }
  redraw();
}

void mousePressed () {
      // revert to previous array. We can only do this once.
  // if someone pressed the key seven times, we can't revert
  // seven times, because the code only has one history state
  // that is updated every time a key is pressed
  // redraw now that our state has changed

  redraw();
}

1 个答案:

答案 0 :(得分:0)

大多数语言从0开始索引(即8个项目将被索引为0到7) - 看起来您正在尝试引用索引为8的数组项,这可能不存在。