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
Page Index Toggle Pages: 1
Null pointer exception (Read 1370 times)
Null pointer exception
Jan 14th, 2010, 8:41am
 
First of all, hello everybody. I'm new to processing, but it has proven to be a very interesting language to play with. I don't like when my toys don't work and that's why I'm here.

When I compile and run my code I get this error:

Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException
     at processing.core.PApplet.main(PApplet.java:6686)
Caused by: java.lang.NullPointerException
     at processing.core.PApplet.background(PApplet.java:8021)
     at paddle.<init>(paddle.java:42)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorI
mpl.java:39)
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorA
ccessorImpl.java:27)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
     at java.lang.Class.newInstance0(Class.java:355)
     at java.lang.Class.newInstance(Class.java:308)
     at processing.core.PApplet.main(PApplet.java:6684)


The code is this:

Code:
int blockX = 50;
int blockY = 20;
int paddleX = 50;
int paddleY = 10;

int TotalBlocksX;
int TotalBlocksY;
int TotalBlocks;
int blockIndex;

block[] blockArray;

int i, j;

void setup()
{
size(500, 500);
TotalBlocksX = int(width / (blockX + 5));
TotalBlocksY = int((0.5*height) / (blockY +5));
TotalBlocks = TotalBlocksX * TotalBlocksY - 1;
blockArray = new block[TotalBlocks];
blockIndex = 0;
}

{
background(200);
rect(mouseX, height-paddleY, paddleX, paddleY);

for (i = 1; i <= TotalBlocksX; i++)
{
for (j = 1; j <= TotalBlocksY; j++)
{
blockArray[blockIndex] = new block(i*(blockX + 5) - (blockX + 5), j*(blockY + 5) - (blockY + 5));
blockArray[blockIndex].create();
blockIndex++;
}
}

blockIndex = 0;


}

class block
{
float locX, locY;

block(int x, int y)
{
locX = x;
locY = y;
}

void create()
{
rect(locX, locY, blockX, blockY);
}
}


The thing is I don't even know on what line the error is happening and as I'm new to OOP I have no idea where to look.

Any help would be appreciated  Cool
Re: Null pointer exception
Reply #1 - Jan 14th, 2010, 9:29am
 
You've missed out the call to draw() after setup - i.e.:

Code:
// <snip />
void setup() {
 size(500, 500);
 TotalBlocksX = int(width / (blockX + 5));
 TotalBlocksY = int((0.5*height) / (blockY +5));
 TotalBlocks = TotalBlocksX * TotalBlocksY;
 blockArray = new block[TotalBlocks];
 blockIndex = 0;
}

void draw(){
 background(200);
 // etc...


You've then got an indexOutOfBounds error to deal with, but it should be easy enough to fix (hint: why do you take 1 away from the calculated value of 'TotalBlocks'?).
Re: Null pointer exception
Reply #2 - Apr 5th, 2010, 10:41am
 
hi there - i'm getting the same error message, but it doesn't seem possible that i've the same cause. this patch was working only a couple of hrs ago, cannot for the life of me figure out what i might have inadvertently changed! any advice / insight would be appreciated... !

code: (Particle4 is a Class object, different patch)

// ----------------------------------------------------------------------
// GLOBAL VARIABLES & IMPORTS
// ----------------------------------------------------------------------

import JMyron.*;
//init external library with var
JMyron m;

float x;//shared by bubbles function & particles3 class
float y;//shared by bubbles function & particles3 class
ArrayList particles;//init arrayList
//Particle4 p;//init class object

//variables for findCentre() custom function
float objx = 160;//animation object in prior frame, x pos
float objy = 120;//animation object in prior frame, y pos
float objNextX = 160;//animation object in current frame, x pos
float objNextY = 120;//animation object in current frame, y pos

//array for reversing display order to create mirror image
int[] imgNormal = m.cameraImage();

// ----------------------------------------------------------------------
// SET-UP & MAIN LOOP
// ----------------------------------------------------------------------

void setup(){//setup OPEN

 size(320,240);//or
 //size(screen.width, screen.height);
 //size(1024, 768); //USE FOR PRESENTATION
 //frameRate(24);//good for sony miniDV but works w/o also
 frame.setLocation(0,0);//for display purposes USE FOR PRESENTATION
 m = new JMyron();//make a new instance of the ext-library object
 m.start(width,height);//start capturing at screen size(320x240)
// for trackColor(red,green,blue,tolerance) tolerance = "sensitivity" of the color matching
 //m.trackColor(0,0,0,100-100);//track black
 m.trackColor(255,255,255,256*3-100);//track white
 //ext library : perpetually update video pixels
 m.update();
 /*adaptivity(float val) sets the speed at which the retina image adapts
  the retina is where the past frames are stored- waiting for comparison with the
  cameraImage in order to calculate the difference */
 m.adaptivity(10);//was 10
 //tells the retinaImage to immediately adapt, completely, to the camera image
 m.adapt();// immediately take a snapshot of the background for differencing
 println("Myron " + m.version());
 rectMode(CENTER);
 noStroke();
 //create arrayList
 particles = new ArrayList();

}//setup CLOSE


void draw(){//draw OPEN

 m.update();//ext library function : perpetually update processed camera frames
 drawCamera();//custom function drawing camera pov
 findCentre();//custom function locating centre x,y's of movement & draws animation

}//draw CLOSE

/*W/WEBCAM IT CRASHES!
void mousePressed(){
 m.settings();//click the window to get the settings
}  */

// ----------------------------------------------------------------------
// CUSTOM FUNCTIONS
// ----------------------------------------------------------------------

void drawCamera(){//drawC OPEN


 //init array for recording difference image (btwn prior & current frames)
 int[] img = m.differenceImage(); //ext lib function : returns the difference image
 //get the normal image of the camera
 loadPixels();
 /*"pixels{} is an array containing the values for all the pixels in the display window.
 These values are of the color datatype, array is the size of the display window.*/
 //cycle through all the pixels and add the frame's pixels to screen
 for(int i = 0; i < width*height; i++){ //for open
   pixels[i] = img[i];
 }//for closed
 
//processing command that updates the display window with the data in the pixels[] array).

 updatePixels();
 
}//drawC CLOSE

////////////////////////// calculations that find average centre of motion's x,y positions
void findCentre() {//findC OPEN

  /*globCenters() returns a list of center points for each glob.
 this is a list of points [point,point,point] */
 //init array (for recording all the centre points of glob in 2d array)
 int[][] centers = m.globCenters();//get the center points
 //draw all the dots while calculating the average.
 //vars for counting the num of average x,y values
 float avX=0;
 float avY=0;
 //cycle thru array and count the num of average x,y pos
 for(int i = 0; i < centers.length; i++){//if1 open
   fill(80);//80
   rect(centers[i][0],centers[i][1],5,5);
   avX += centers[i][0];
   avY += centers[i][1];
 }//if1 closed
 //restrict average x,y data to 1 less than array length
 if(centers.length-1 > 0){//if2 open
   avX/=centers.length-1;
   avY/=centers.length-1;
 }//if2 closed

 //draw the average of all the points in red.
 fill(255,0,0);
 rect(avX,avY,5,5);

 //update the location of the object on the screen.
 if(!(avX==0&&avY==0)&&centers.length>0){
   objNextX = avX;
   objNextY = avY;
 }
 objx += (objNextX-objx)/10.0f;
 objy += (objNextY-objy)/10.0f;
 fill(30,100,0,0);
 ellipseMode(CENTER);
 ellipse(objx,objy,30,30);

 bubbles(objx,objy);
 
}//findC CLOSE

////////////////////// add class object
void bubbles(float x, float y) {//bubbles OPEN
 
 // A new Particle object is added to the ArrayList every cycle through draw().
 particles.add( new Particle4(x,y) );
 // Iterate through our ArrayList and get each Particle
 // The ArrayList keeps track of the total number of particles.
 for (int i = 0; i < particles.size(); i++ ) {//for open
   Particle4 p = (Particle4) particles.get(i);
   p.run();
   p.gravity();
   p.display();
 }//for closed
 
 // If the ArrayList has more than 100 elements in it, we delete the first element, using remove().
 if (particles.size() > 1000) {//if open
   particles.remove(0);
 }//if closed
 
}//bubbles CLOSE

////////////////////////// needed for control over external library init
public void stop(){//stop OPEN
 m.stop();//stop the object
 super.stop();
}//stop CLOSE
Re: Null pointer exception
Reply #3 - Apr 5th, 2010, 10:51am
 
nevermind - found the bug ! didn't notice it until i cut and paste the code in here: a left over array from trying to create a mirror output that didn't work.

if anyone knows how to do this (output a mirror image while differencing with Myron + processing); i would be grateful for the feedback!
Page Index Toggle Pages: 1