pd
YaBB Newbies
Offline
Posts: 9
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)&¢ers.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