macOS question pixels[loc] issue

please format code with </> button * homework policy * asking questions

i used macOS and processing 3 ver(3.5.4)

before i have video library issue so fix that

link

but i have one more issue

void draw() {
//image(cam, 0, 0);

cam.loadPixels();
prevFrame.loadPixels();

for(int x=0; x<cam.width; x++) {
for(int y=0; y<cam.height; y++) {
int loc = x + y*cam.width;
float r = red(cam.pixels[loc]);
float g = green(cam.pixels[loc]);
float b = blue(cam.pixels[loc]);
float tr = red(prevFrame.pixels[loc]);
float tg = green(prevFrame.pixels[loc]);
float tb = blue(prevFrame.pixels[loc]);

  float d =dist(r, g, b, tr, tg, tb);
  
  if(d > 100) {
    pixels[loc] = color(255);
  } else {
    pixels[loc] = color(0); //error here
  }

  
}

}

updatePixels();
}

i make that code

pixels[loc] = color(255); <—this range is ok
} else {
pixels[loc] = color(0); <---- but this range is error about “NullPointerException”
}

how can fix this error …? help me please
maybe i think my macOS don’t use ‘pixels[loc]’ two times
ps. sorry my english ability is not good…

thanks

When you work with pixel arrays (note plural) you need to be careful to reference the right pixel array. You have three pixel arrays in you’re code:

  • cam.Pixels
  • prevFrame.Pixels
  • pixels – that is pixel array for the screen or window for processing app

You define loc from cam, if it has different height or width than display you will get “NullPointerException” when you pick a pixel on area that’s within cam area, but outside of display area.

1 Like

thanks to answer i will must fix this issue thank you so much :blush: