We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Null Point Exception, again...
Page Index Toggle Pages: 1
Null Point Exception, again... (Read 762 times)
Null Point Exception, again...
Dec 10th, 2009, 12:45pm
 
I don't know what to do anymore... Here's the code, line 114 (for(int i = timer.length; i >= 0; i--)...):

Code:

int screenSize, readScore;
int[] timer;
int[] score;

boolean startGame;
boolean pauseGame;
boolean endGame;

SPACE zone;
AVATAR player;

void setup()
{
 screenSize = 400;
 size (screenSize,screenSize);
 frameRate(30);
 
 int[] timer = new int [3];
 int[] score = new int [3];
 
 startGame = false;
 pauseGame = false;
 endGame = false;
 
 zone = new SPACE
 (
 loadImage("figura1.png"), loadImage("figura2.png"),
 loadImage("figura3.png"), color(27,214,219), (screenSize/2),
 (screenSize/2), (screenSize/15)
 );
 
 player = new AVATAR
 (
 color(31,155,16), (screenSize/2), (screenSize/2),
 (screenSize/15),(screenSize/80)
 );
}

void mousePressed()
{
 startGame =! startGame;
 pauseGame = true;
 
 if(endGame == true)
 {
   endGame = false;
 }
}

void keyPressed()
{
 if (key == 't')
 {
   endGame =! endGame;
 }
}

void draw()
{
 background(color(#141B5D));
 smooth();
 zone.display();
 
 if (endGame == false)
 {
   player.display();
   
   if (startGame == true)
   {
     zone.alphaTest = true;
     player.move();
     
     //TIMER
     timer[0]++;
     if (timer[0] > 9)
     {
       timer[1]++;
       timer[0] = 0;
       
       if(timer [1] > 9)
       {
         timer[2]++;
         timer[1] = 0;
         
         if(timer[2] > 9)
         {
           timer[3]++;
           timer[2] = 0;
           
           if(timer[3] > 9)
           {
             for(int i = 0; i <= timer.length; i++)
             {
               timer [i] = 9;
             }
           }
         }
       }
     }
   }
   
   if(startGame == false && pauseGame == true)
   {
     zone.pause();
   }
 }
 
 if(endGame = true)
 {
   pauseGame = false;    
   player.end();
   zone.end();
   
   for(int i = timer.length; i >= 0; i--)
   {
     score[readScore] = timer[i];
     readScore++;
   }
   
   for(int i = 0; i <= timer.length; i++)
   {
     timer[i] = 0;
   }
 }
}


Thanks in advance.
Re: Null Point Exception, again...
Reply #1 - Dec 10th, 2009, 12:56pm
 
hmm timer.length is only 3, right? so there is no timer[3]; only 0,1,2
Re: Null Point Exception, again...
Reply #2 - Dec 10th, 2009, 12:58pm
 
An NPE, really? I expect more of an ArrayIndexOutOfBounds exception:
Code:
int[] timer = new int [3];
[...]
timer[3]++;

is prone to fail.
So are the check i <= timer.length and the assignment int i = timer.length.
An array initialized at size n goes from 0 to n-1.

[EDIT] Good catch, koogy! I missed the double declaration, too!
Re: Null Point Exception, again...
Reply #3 - Dec 10th, 2009, 12:59pm
 
you define a global int[] timer

but you are also defining and initialising a new int[] timer within setup() and this is restricted in scope to only setup.

you then use the uninitialised timer in the draw. NPE...

remove the int[] before timer (and score) in setup(), you don't need it.




the next problem you will hit is here:

for(int i = 0; i <= timer.length; i++) {
 timer [i] = 9;
}

use < in the condition.

an array of size 10 has elements numbered from 0 to 9

your loop goes from 0 to 10 because of the equals. 10 > 9... hello Array Index Out Of Bounds Exception!

(curses, too slow again!)

(oh, ok, they both went for the out of bounds exception and didn't spot the local variable. oddly, that was the first thing i saw too)
Re: Null Point Exception, again...
Reply #4 - Dec 10th, 2009, 1:26pm
 
Thanks everyone.
Page Index Toggle Pages: 1