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 & HelpSyntax Questions › Null Pointer exception of class opbject
Page Index Toggle Pages: 1
Null Pointer exception of class opbject (Read 706 times)
Null Pointer exception of class opbject
Jan 31st, 2010, 2:13pm
 
Hey fairly new at this and im trying to find out why my code would be giving be a null pointer exception, im not really sure what this error code means so i'm having alot of trouble solving it heres my code;

Roomba player;
void setup() {
 size(600,600);
}

void draw() {
 // blank the background
 background(0);
 player.drawRoomba(); -This is where i get the error

}
class Roomba {
 int roombaX;
 int roombaY;
 int dX;
 int dY;

 Roomba () {
   roombaX = 300;
   roombaY = 300;
   dX = 0;
   dY = 0;
 }

 void drawRoomba() {
   fill(50,125,10,150);
   ellipse(roombaX,roombaY,50,50);

   // move the player
   roombaX += dX;
   roombaY += dY;
 }  

 void keyPressed() {
   // up
   if (key == 'i') {
     dY--;
   }
   // down
   if (key == 'k') {
     dY++;
   }
   // left
   if (key == 'j') {
     dX--;
   }
   // right
   if (key == 'l') {
     dX++;
   }
   // press space for brakes!
   if (key == ' ') {
     dX = (int)(dX * 0.9);
     dY = (int)(dY * 0.9);
   }
   if (roombaX < 0 || roombaX > 600) {
     dX *= -1;
   }
   if (roombaY < 0 || roombaY > 600) {
     dY *= -1;
   }
 }

}

Any help would be greatly appreciated im having alot of trouble here.

Re: Null Pointer exception of class opbject
Reply #1 - Jan 31st, 2010, 8:04pm
 
Simply declaring a Roomba object ("Roomba player;") does not actually create that object. Instead, you have to make a new one. Try "Roomba player = new Roomba();" instead.
Re: Null Pointer exception of class opbject
Reply #2 - Feb 1st, 2010, 12:04pm
 
when you do new Roomba(); in your setup, it means that you allocate memory to that object, so it will remember whats in it.
thats a reason why we use objects, we need to keep specific informations in memory for each objects.
Page Index Toggle Pages: 1