# Receiving error but cannot debug: ArrayIndexOutOfBoundsException

**URL:** <https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415>\
**Category:** Libraries\
**Created:** [March 5, 2020, 12:38pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415 "2020-03-05T12:38:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 5, 2020, 12:38pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/1 "2020-03-05T12:38:12Z")

</div>

```auto
int currentScreen;
PImage img;
PImage img0;
PImage img1;
import processing.sound.*;
SoundFile file;

void setup() {
  size (1000, 683);
  noStroke();
  smooth();
  img = loadImage ("batman.jpeg");
  img0 = loadImage ("joker.jpg");
  img1 = loadImage("jolaugh.jpg");
  file = new SoundFile(this, "jlaugh.mp3");
}

void draw() {
  
  switch (currentScreen) {
    case 1: keyPressedOne(); break;
    case 2: keyReleasedOne(); break;
    //case 3: keyReleasedZero(); break;
    default: image(img, 0, 0); break;
  }
}

void mousePressed() {
  currentScreen++;
  if (currentScreen > 2) {currentScreen = 0;}
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == 'a'){
      int x = int(random(img.width));
      int y = int(random(img.height));
      color pix = img.get(x, y);
      fill(pix, 128);
      ellipse(x, y, 50, 50);
    }
} else {
  image(img, 0, 0);
  }
}

void keyReleasedOne() {
  loadPixels();
  if (key == '0'){
    for (int x = 0; x < img1.width; x++ ) {
    for (int y = 0; y < img1.height; y++ ) {
    
    
    int loc = x + y*img1.width;
      float r = red (img1.pixels[loc]);
      float g = green(img1.pixels[loc]);
      float b = blue (img1.pixels[loc]);
      
      float adjustBrightness = map(mouseX, 0, width, 0, 8); 
      //r *= adjustBrightness;
      //g *= adjustBrightness;
      //b *= adjustBrightness;
      
      r = constrain(r, 0, 255); 
      g = constrain(g, 0, 255);
      b = constrain(b, 0, 255);
      
      color c = color(r, g, b);
      if(y > 200)
      c = color(200, g, b);
      pixels[loc] = c;
      //img1.resize(1000,683);
      //image(img1,0 ,0);
      //file.play();
      //noLoop();
      }
    }
    
     
  } else {
    img0.resize(1000, 683);
    image(img0, 0,0);
    }
       updatePixels();
  }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 5, 2020, 2:46pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/2 "2020-03-05T14:46:16Z")

</div>

Hello and welcome to the forum!

Great to have you here!

Do you mean, the code runs but does not all you want it to?

Or rather does not run? What is the error message then?

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 6, 2020, 8:08am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/3 "2020-03-06T08:08:26Z")

</div>

ArrayIndexOutOfBoundsException: 683520

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 6, 2020, 8:30am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/4 "2020-03-06T08:30:10Z")

</div>

in which line?

Which line is highlighted with the error?

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 6, 2020, 8:32am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/5 "2020-03-06T08:32:45Z")

</div>

pixels[loc] = c; \<-- this is the line

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [March 6, 2020, 8:34am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/6 "2020-03-06T08:34:23Z")

</div>

I change a little bit your code, you need another switch in draw method.  
Maybe your error is a problem in path loading outside file

```auto
import processing.sound.*;
int currentScreen;
PImage img;
PImage img0;
PImage img1;
color pix;
int x, y;
SoundFile file;
void setup() {
    size (1000, 683);
    noStroke();
    smooth();
    img = loadImage ("batman.jpeg");
    img0 = loadImage ("joker.jpg");
    img1 = loadImage("jolaugh.jpg");
    file = new SoundFile(this, "jlaugh.mp3");
}

void draw() {
    switch (currentScreen) {
    case 1:
        fill(pix, 128);
        ellipse(x, y, 50, 50);
        break;
    case 2:
        image(img1, 0, 0);
        break;
    default :
     image(img, 0, 0); 
        break;
    }
}

void mousePressed() {
    currentScreen++;
    if (currentScreen > 2) {
        currentScreen = 0;
    }
    switch (currentScreen) {
    case 1: 
        keyPressedOne(); 
        break;
    case 2: 
        keyReleasedOne(); 
        break;
        //case 3: keyReleasedZero(); break;
    default: 
        break;
    }
}

void keyPressedOne() {
    if (key == 'a') {
        x = int(random(img.width));
        y = int(random(img.height));
        pix = img.get(x, y);
    }
}

void keyReleasedOne() {
    loadPixels();
    if (key == '0') {
        for (int x = 0; x < img1.width; x++ ) {
            for (int y = 0; y < img1.height; y++ ) {

                int loc = x + y*img1.width;
                float r = red (img1.pixels[loc]);
                float g = green(img1.pixels[loc]);
                float b = blue (img1.pixels[loc]);

                float adjustBrightness = map(mouseX, 0, width, 0, 8); 
                //r *= adjustBrightness;
                //g *= adjustBrightness;
                //b *= adjustBrightness;

                r = constrain(r, 0, 255); 
                g = constrain(g, 0, 255);
                b = constrain(b, 0, 255);

                color c = color(r, g, b);
                if (y > 200)
                    c = color(200, g, b);
                pixels[loc] = c;
            }
        }
    }  
    updatePixels();
    img1.resize(1000, 683);
    file.play();
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 6, 2020, 8:34am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/7 "2020-03-06T08:34:45Z")

</div>

> [@firetryer](#):
>
> pixels[loc] = c;

I think you want `img1.pixels[loc] = c;`

you forgot img1

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [March 6, 2020, 8:45am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/8 "2020-03-06T08:45:44Z")

</div>

I don’t understand this part of code.  
I think if you do this you must also :

```auto
img1.loadPixels();
//and
img1.updatePixels();

```

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 6, 2020, 9:01am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/9 "2020-03-06T09:01:47Z")

</div>

```auto
int currentScreen;
PImage img;
PImage img0;
PImage img1;
import processing.sound.*;
SoundFile file;

void setup() {
  size (1000, 683);
  noStroke();
  smooth();
  img = loadImage ("batman.jpeg");
  img0 = loadImage ("joker.jpg");
  img1 = loadImage("jolaugh.jpg");
  file = new SoundFile(this, "jlaugh.mp3");
}

void draw() {
  
  switch (currentScreen) {
    case 1: keyPressedOne(); break;
    case 2: keyReleasedOne(); break;
    //case 3: keyReleasedZero(); break;
    default: image(img, 0, 0); break;
  }
}

void mousePressed() {
  currentScreen++;
  if (currentScreen > 2) {currentScreen = 0;}
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == 'a'){
      int x = int(random(img.width));
      int y = int(random(img.height));
      color pix = img.get(x, y);
      fill(pix, 128);
      ellipse(x, y, 50, 50);
    }
} else {
  background (200);
  }
}

void keyReleasedOne() {
  img1.loadPixels();
  if (keyPressed) {
  if (key == '0'){
    for (int x = 0; x < img1.width; x++ ) {
    for (int y = 0; y < img1.height; y++ ) {
    
    
    int loc = x + y*img1.width;
    
    
      float r = red (img1.pixels[loc]);
      float g = green(img1.pixels[loc]);
      float b = blue (img1.pixels[loc]);
      
     float adjustBrightness = map(mouseX, 0, width, 0, 8); 
      //r *= adjustBrightness;
      //g *= adjustBrightness;
      //b *= adjustBrightness;
      
      r = constrain(r, 0, 255); 
      g = constrain(g, 0, 255);
      b = constrain(b, 0, 255);
      
      color c = color(r, g, b);
      if(y > 200)
      c = color(200, g, b);
      
      
      img1.pixels[loc] = c;
      img1.resize(1000,683);
      image(img1,0 ,0);
    }
      
      }
    }
    
    img1.updatePixels();
    file.play();
    noLoop();
     
  } else { 
    
    img0.resize(1000, 683);
    image(img0, 0,0);
    }
    
  }

```

I tried this one but then the code gets stuck and not getting error this time. I need both images in if and else. Thank you for trying, really appreciated

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 6, 2020, 9:31am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/10 "2020-03-06T09:31:57Z")

</div>

I tried this one, did not make any difference

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 6, 2020, 9:32am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/11 "2020-03-06T09:32:37Z")

</div>

@Chrisir it did not work

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 6, 2020, 11:07am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/12 "2020-03-06T11:07:37Z")

</div>

this seems to do something.

- initially currentScreen is 0, so the default part of switch in draw() is called!!!

- When you click the mouse once, currentScreen is 1 and you can click “a” there. Please note that the change is appears only briefly since it is inside the if-clause.

- When you click the mouse again, currentScreen is 2 and you can click “0” there

Chrisir

```auto

int currentScreen;
PImage img;
PImage img0;
PImage img1;

import processing.sound.*;
SoundFile file;

//-----------------------------------------------------

void setup() {
  size (1000, 683);
  noStroke();
  smooth();

  img = loadImage ("batman.jpeg");
  img0 = loadImage ("joker.jpg");
  img1 = loadImage("jolaugh.jpg");

  file = new SoundFile(this, "jlaugh.mp3");
}

void draw() {

  switch (currentScreen) {

  case 1: 
    keyPressedOne(); 
    break;

  case 2: 
    keyReleasedOne(); 
    break;

    //case 3: keyReleasedZero(); break;

  default: 
    image(img, 0, 0); 
    break;
  }//switch

  fill(255); 
  text(currentScreen, width-23, 23);
}//func 

void mousePressed() {
  currentScreen++;
  if (currentScreen > 2) {
    currentScreen = 0;
  }
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == 'a') {
      int x = int(random(img.width));
      int y = int(random(img.height));
      color pix = img.get(x, y);
      fill(pix, 128);
      ellipse(x, y, 50, 50);
    }
  } else {
    background (200);
  }
}

void keyReleasedOne() {

  if (keyPressed) {
    if (key == '0') {
      img1.loadPixels();
      for (int x = 0; x < img1.width; x++ ) {
        for (int y = 0; y < img1.height; y++ ) {

          int loc = x + y*img1.width;

          float r = red (img1.pixels[loc]);
          float g = green(img1.pixels[loc]);
          float b = blue (img1.pixels[loc]);

          float adjustBrightness = map(mouseX, 0, width, 0, 8); 
          //r *= adjustBrightness;
          //g *= adjustBrightness;
          //b *= adjustBrightness;

          r = constrain(r, 0, 255); 
          g = constrain(g, 0, 255);
          b = constrain(b, 0, 255);

          color c = color(r, g, b);
          if (y > 200)
            c = color(200, g, b);

          img1.pixels[loc] = c;
        }
      }//for 
      img1.updatePixels();
      img1.resize(1000, 683);
      image(img1, 0, 0);
    }

    //file.play();
    //noLoop();
  } else {

    img0.resize(1000, 683);
    image(img0, 0, 0);
  }
}

```

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 6, 2020, 11:13am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/13 "2020-03-06T11:13:40Z")

</div>

works better, but if you enable sound it get’s stuck on “img1”

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 6, 2020, 11:15am UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/14 "2020-03-06T11:15:48Z")

</div>

> [@Chrisir](#):
>
> [//file.play](https://file.play)();  
> //noLoop();

did you comment out those **2** lines?

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 6, 2020, 12:03pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/15 "2020-03-06T12:03:54Z")

</div>

when I uncomment it, it got stuck again

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 6, 2020, 2:36pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/16 "2020-03-06T14:36:20Z")

</div>

So noLoop stops entire program. Do you want this? No.

And what error do you get at which line?

Sure that the audio file has the right name when you load it? I haven’t tried.

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 6, 2020, 3:42pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/18 "2020-03-06T15:42:16Z")

</div>

```auto
int currentScreen;
PImage img;
PImage img0;
PImage img1;
PImage img2;
PImage img3;

import processing.sound.*;
SoundFile file, file2;

void setup() {
  size (1000, 683);
  //noStroke();

  img = loadImage ("batman.jpeg");
  img0 = loadImage ("joker.jpg");
  img0.resize(1000, 683);
  
  img1 = loadImage("jolaugh.jpg");
  img1.resize(1000, 683);
  
  img2 = loadImage("riddler.jpeg");
  img2.resize(1000, 683);
  
  img3 = loadImage("rid.png");
  img3.resize(200,300);

  file = new SoundFile(this, "jlaugh.mp3");
  file2 = new SoundFile(this, "rmt.mp3");
}

void draw() {

  switch (currentScreen) {

  case 1: 
    keyPressedOne(); 
    break;

  case 2: 
    keyReleasedOne(); 
    break;

    case 3: 
    keyReleasedTwo(); 
    break;

  default: 
    image(img, 0, 0); 
    break;
  }

  fill(255); 
  text(currentScreen, width-23, 23);
}

void mousePressed() {
  currentScreen++;
  if (currentScreen > 4) {
    currentScreen = 0;
  }
}

void keyPressedOne() {
  if (keyPressed) {
    if (key == 'a') {
      int x = int(random(img.width));
      int y = int(random(img.height));
      color pix = img.get(x, y);
      fill(pix, 128);
      ellipse(x, y, 50, 50);
    }
  } else {
    background (200);
  }
}

void keyReleasedOne() {

  if (keyPressed) {
    if (key == '1') {
      img1.loadPixels();
      for (int x = 0; x < img1.width; x++ ) {
        for (int y = 0; y < img1.height; y++ ) {

          int loc = x + y*img1.width;

          float r = red (img1.pixels[loc]);
          float g = green(img1.pixels[loc]);
          float b = blue (img1.pixels[loc]);

          //float adjustBrightness = map(mouseX, 0, width, 0, 8); 
          ////r *= adjustBrightness;
          ////g *= adjustBrightness;
          ////b *= adjustBrightness;
          
          color c = color(r, g, b);
          if (y > mouseY)
            c = color(200, random(60,100), r, 0);
          if (x > mouseX/2)
            c = color(random(0,150),b, 50, 0);
          if (x > mouseX)
            c = color(r/2+62, b*1.5, g/2+50, 0);
            
          r = constrain(r, 0, 255); 
          g = constrain(g, 0, 255);
          b = constrain(b, 0, 255);
          
          //noStroke();
          //fill(c);
          //ellipse(x,y,10,10);

         img1.pixels[loc] = c;
         
        }
      }//for 
      img1.updatePixels();
      image(img1, 0, 0);
    }

    if(!file.isPlaying()){
    file.play();
    }
  } else {

    img0.resize(1000, 683);
    image(img0, 0, 0);
  }
}

void keyReleasedTwo() {
  if (keyPressed){
    if ( key == '2') {
      background(img2);
      //blend(img3, 0, 0, 33, 100, 67, 0, 33, 100, ADD);
      //blend(img3, 0, 0, 33, 100, 67, 0, 33, 100, ADD);
      
      int i = 1;
      while (i < 100) {
        image(img3, i*10, i*5);
          i = i + 20;
      } 
      if(!file2.isPlaying()){
      file2.play();
      }
    }
  } else {
    image(img2, 0, 0);
  }
}

```

**NEED HELP WITH ‘IMG1’ PIXEL DISPLAY**

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 6, 2020, 3:45pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/19 "2020-03-06T15:45:19Z")

</div>

You have to give a longer description:

What happens now falsely, what do you want to happen instead?

Please note that some of the stuff is inside if-clauses. So this stuff shows up only while you hold down the key.

When you don’t want this, do the changes of pixels [] still inside the if, but image() and ellipse outside it

---

<div class="post-metadata">

**Author:** ![firetryer](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@firetryer](https://discourse.processing.org/u/firetryer)\
**Post date:** [March 6, 2020, 3:51pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/20 "2020-03-06T15:51:32Z")

</div>

what do you suggest? As I cannot achieve the goal as the image hides behind the effect but the effect is not transparent and hides the picture.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 6, 2020, 4:01pm UTC](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415/21 "2020-03-06T16:01:20Z")

</div>

Ah, okay, when you want it so that the other image is shown only when key hold, okay

Alternatively, use key 0 to toggle between one view and the other - press key shortly, view gets changed permanently. Use a boolean variable viewChangedImage

[Next page](https://discourse.processing.org/t/receiving-error-but-cannot-debug-arrayindexoutofboundsexception/18415.md?page=2)
