NullPointerExpection in for loop

for(int i = maxlayers+1; i > 0; i-=1) {
      image(layers[i+1], xpos, ypos, xsize*zoomx, ysize*zoomy);
    }

This code for some reason just return “NPE NullPointerExpection” why? i dont know! thats why im asking you! Arys

because the array is not big enough i.e. no index value at layers[ maxlayers + 2] , when i = 1

for(int i = layers.length - 1; i > 0; i-=1) {
      image(layers[i], xpos, ypos, xsize*zoomx, ysize*zoomy);
}
2 Likes

I assume that there is an image at layers[0] in which case this is better

for(int i = layers.length - 1; i >= 0; i--) {
      image(layers[i], xpos, ypos, xsize*zoomx, ysize*zoomy);
}

i-- decrements i by 1

2 Likes

thank you! it realy helps!