problem accessing to the array in a loop

I got an error as you can see below…


Rectangle rGreenBorder[] = new Rectangle[greenBorder.length]; // length 2
for (int i = 0; i < greenBorder.length; i++) {
    Border border = (Border) greenBorder[i];
    rGreenBorder[i] = border.getBounds();
    
    // Exception in thread "Thread-1" java.lang.NullPointerException 
    // at java.awt.Rectangle.intersects(Rectangle.java:786)
    if (rPlayer.intersects(rGreenBorder[0])) {
          player.setX(player.getWidth() + 15);
    }
    if (rPlayer.intersects(rGreenBorder[1])) {
          player.setX(BOARD_WIDTH - player.getWidth() - 15);
    }
    
    // passing i var into square brackets works.. but that's not the point i want
    if (rPlayer.intersects(rGreenBorder[i])) {
          player.setX(BOARD_WIDTH - player.getWidth() - 15);
    }
}


Most NPEs are pretty simple, but this one is actually kind of tricky. Since you know rPlayer isn’t null (otherwise the NPE would be thrown from there and not rPlayer.intersects), it remains that rGreenBorder[0] is null. It’s easy to prove you initialized it in the first pass through the loop with border.getBounds(), so what remains is that getBounds() is returning null.

What’s the class of greenBorder[0]? Does it override getBounds?

You’re going to want to put a breakpoint on line 3 and step through with a debugger to figure out exactly what’s going on.

Instead of declaring each

Rectangle rGreenBorder[i] = (Rectangle) greenBorder[i].getBounds();

I decided to go for less lines but few chars longer if condition… :smiley:


if (rPlayer.intersects(greenBorder[0].getBounds()))

However, let’s figure out what’s the problem, so that I can know solution:
At begin of Board class, I’ve declared:


private Border blueBorder[] = new Border[2];
private Border redBorder[] = new Border[2];
private Border greenBorder[] = new Border[2];

As well I initialized them:


for (int i = 0; i < 1; i++) {
    for (int j = 0; j < 2; j++) {  
           redBorder[j] = new Border("border_red.png", j * (BOARD_WIDTH - 110), i * 300);
           blueBorder[j] = new Border("border_blue.png", j * (BOARD_WIDTH - 110), BOARD_HEIGTH - 15);
           greenBorder[j] = new Border("border_green.png", j * 345, 15);
    }
}

So, they’re all using the same class, and as you can see where I init them, it’s constructor which receives parameters (image, posX, posY).
What do you think is solution ?

Actually, I think I might have found your error here. First, you start be initializing the loop here…
[icode]Rectangle rGreenBorder[] = new Rectangle[greenBorder.length]; // length 2[/icode]

Then you enter the loop, and initialize the first variable here…


for (int i = 0; i < greenBorder.length; i++) {
    Border border = (Border) greenBorder[i];
    rGreenBorder[i] = border.getBounds();
   ...

But, since you do this within the loop, only the rGreenBorder[0] is initialized. Your NPE error has to be here…

 
...
if (rPlayer.intersects(rGreenBorder[1])) {
      player.setX(BOARD_WIDTH - player.getWidth() - 15);
}

because you never got around the second iteration of the loop to initialize that variable. To fix… it is actually pretty easy, just do this instead…


Rectangle rGreenBorder[] = new Rectangle[greenBorder.length]; // length 2
for (int i = 0; i < greenBorder.length; i++) {
    Border border = (Border) greenBorder[i];
    rGreenBorder[i] = border.getBounds();
    
    // Exception in thread "Thread-1" java.lang.NullPointerException 
    // at java.awt.Rectangle.intersects(Rectangle.java:786)
    if (rPlayer.intersects(rGreenBorder[0])) {
          player.setX(player.getWidth() + 15);
    }
    if( i > 0){//This should prevent any NullPointerException errors...
          if (rPlayer.intersects(rGreenBorder[1])) {
                 player.setX(BOARD_WIDTH - player.getWidth() - 15);
          }
    }
    // passing i var into square brackets works.. but that's not the point i want
    if (rPlayer.intersects(rGreenBorder[i])) {
          player.setX(BOARD_WIDTH - player.getWidth() - 15);
    }
}

I’ve realized the compiler usually has a hard time pin-pointing these errors in arrays. I usually do breakpoints to find out exact position of errors. In this case, it is just that you checked a variable within a loop that wasn’t initialized yet.