Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  problem accessing to the array in a loop  (Read 982 times)
0 Members and 1 Guest are viewing this topic.
Offline Dicee

Junior Devvie


Medals: 2



« Posted 2013-02-16 13:55:02 »

I got an error as you can see below...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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);
    }
}

Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2013-02-16 16:49:21 »

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.
Offline Dicee

Junior Devvie


Medals: 2



« Reply #2 - Posted 2013-02-16 22:04:03 »

Instead of declaring each
1  
Rectangle rGreenBorder[i] = (Rectangle) greenBorder[i].getBounds();
I decided to go for less lines but few chars longer if condition.. Cheesy
1  
if (rPlayer.intersects(greenBorder[0].getBounds()))
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dicee

Junior Devvie


Medals: 2



« Reply #3 - Posted 2013-02-16 22:54:05 »

However, let's figure out what's the problem, so that I can know solution:
At begin of Board class, I've declared:
1  
2  
3  
private Border blueBorder[] = new Border[2];
private Border redBorder[] = new Border[2];
private Border greenBorder[] = new Border[2];


As well I initialized them:
1  
2  
3  
4  
5  
6  
7  
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 ?
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #4 - Posted 2013-02-17 00:03:55 »

I got an error as you can see below...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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);
    }
}


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


Then you enter the loop, and initialize the first variable here...
1  
2  
3  
4  
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...
1  
2  
3  
4  
5  
 
...
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...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!