Question

I'm trying to make a graph in processing, but keep getting the "ArrayIndexOutOfBounds" error. I am having a hard time understanding the concept of the map() syntax, could anyone clear it up for me? I'm not sure what every number is supposed to stand for within map(x,y,z,a,b);

String[] name = {
  "1st:",
  "5th:",
  "10th:",
  "15th:",
  "20th:",
  "25th:",
  "30th:"
};

int[] temperature = {
  81,
  82,
  84,
  85,
  87,
  88,
  90
};


void setup(){
  size(200,200);
}

void draw(){
  int x=0;
  for(int i=0;i<10;i++){

    if(mouseX>x && mouseX<=x+40){
      fill(255,40,40);
    }else{
      fill(50);
    }

    float h = map(temperature[i], 0, 100, 0, 200);

    rect(x+4,height-h,32,h);

    x+=40;
  }

}
Was it helpful?

Solution

Your arrays have 7 elements, but you iterate 10 times.

for(int i=0;i<10;i++){
  ...
  float h = map(temperature[i], 0, 100, 0, 200);

}

Either you fill up your arrays with additional 3 elements, or better: Instead of the number 10 you should use i < temperature.length() as condition in for-loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top