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 › Syntax error when compiling
Page Index Toggle Pages: 1
Syntax error when compiling (Read 234 times)
Syntax error when compiling
Jun 5th, 2009, 8:44pm
 
Hi,

When i try to compile the following code :

class Point {

 int x, y, z;
 
 Point(int _x, int _y) {
   x = _x;
   y = _y;
   z = 0;
 }
}

import JMyron.*;
JMyron m;
PImage img1, img2;
// img1 and img2 are the location to temporarily store the previous
// and current frame.
final int NROWS = 18;
final int NCOLS = 24;
final float LIMIT = 40.0;
int w, h;
// w and h are the size of the grid.

void initGrid() {
  for (int r=0;r<=NROWS;r++) {
     for (int c=0;c<=NCOLS;c++) {
        grid[r][c] = new Point(c*w,r*h);
     }
  }
}

void mapGrid(PImage _i) {
 noStroke();
 for (int r=0;r<NROWS;r++) {
   beginShape(QUAD_STRIP);
   texture(_i);
   for (int c=0;c<=NCOLS;c++) {
     vertex(grid[r][c].x,grid[r][c].y,grid[r][c].z,
     c*w,r*h);
     vertex(grid[r+1][c].x,grid[r+1][c].y,grid[r+1][c].z,
     c*w,(r+1)*h);
   }
   endShape();
 }
}

Point [][] grid = new Point[NROWS+1][NCOLS+1];

void setup() {
  size(320,240);
  m = new JMyron();
  m.start(width,height);
  println(m.getForcedWidth());
  println(m.getForcedHeight());
  m.findGlobs(0);
  img1 = createImage(width,height,ARGB);
  img2 = createImage(width,height,ARGB);
  noFill();
  stroke(255,200,0);
  smooth();
  frameRate(15);
  w = width/NCOLS;
  h = height/NROWS;
}

void draw() {
  background(0);
// copy the image from img1 to img2 - the previous frame.
  img2.copy(img1,0,0,img1.width,img1.height,
   0,0,img2.width,img2.height);
  img2.updatePixels();
  m.update();
// update the current frame to img1.
  m.imageCopy(img1.pixels);
  img1.updatePixels();
  image(img1,0,0);
  findFlow();
}

void findFlow() {
 int xOff = w/2;
 int yOff = h/2;
 for (int r=1;r<NROWS;r++) {
   for (int c=1;c<NCOLS;c++) {
     Point p1 = new Point(c*w, r*h);
     Point p2 = findPoint(p1.x, p1.y, xOff, yOff);
     drawLine(p1,p2);
     grid[r][c].z = int(dist(p1.x,p1.y,p2.x,p2.y));
   }
 }
}

void drawLine(Point _p, Point _q) {
// draw the arrow line from point _q to point _p.
  if (_p.x!=_q.x || _p.y!=_q.y) {
    line(_p.x,_p.y,_q.x,_q.y);
    float ang = atan2(_q.y-_p.y,_q.x-_p.x);
    float ln = w/3.0;
    float tx = _p.x + ln*cos(ang-PI/6);
    float ty = _p.y + ln*sin(ang-PI/6);
    line(_p.x,_p.y,tx,ty);
    tx = _p.x + ln*cos(ang+PI/6);
    ty = _p.y + ln*sin(ang+PI/6);
    line(_p.x,_p.y,tx,ty);
  }
  else {
    line(_p.x,_p.y,_q.x,_q.y);
  }
}

Point findPoint(int _x, int _y, int _xo, int _yo) {

// Given a pixel (_x, _y) in img1, we search the neighborhood of that
// pixel in img2 and try to find a matching colour.
//
// The neighborhood size is defined by (w x h) and the boundaries are
// x0 - left
// x1 - right
// y0 - top
// y1 - right

  int x0 = _x - _xo;
  int x1 = _x + _xo;
  int y0 = _y - _yo;
  int y1 = _y + _yo;

// Initialize the minimum difference to a high value.
// Loop through the pixels in img2 within the boundary.
// Find the pixel with minimum difference from the original one
// in img1.

  float minDiff = 999999999;
  Point p = new Point(_x,_y);
  color c1 = img1.pixels[_y*img1.width+_x];
  color c2 = img2.pixels[_y*img2.width+_x];
  if (!matchCol(c1,c2)) {
     for (int r=y0;r<y1;r++) {
       for (int c=x0;c<x1;c++) {
         c2 = img2.pixels[r*img2.width+c];
         float diff = dist(red(c1),green(c1),blue(c1),
         red(c2),green(c2),blue(c2));
         if (diff<minDiff) {
           minDiff = diff;
           p.x = c;
           p.y = r;
         }
       }
     }
  }
  return p;
}

boolean matchCol(color c1, color c2) {
// Compare two colour values and see if they are similar.
  float d = dist(red(c1),green(c1),blue(c1),
    red(c2),green(c2),blue(c2));
  return (d<LIMIT);
}



I get the following error :

Exception in thread "Animation Thread" java.lang.NullPointerException
     at TySke.findFlow(TySke.java:105)
     at TySke.draw(TySke.java:94)
     at processing.core.PApplet.handleDraw(PApplet.java:1406)
     at processing.core.PApplet.run(PApplet.java:1311)
     at java.lang.Thread.run(Thread.java:613)

Does anybody know what this means or how to fix it?
Re: Syntax error when compiling
Reply #1 - Jun 6th, 2009, 1:48am
 
Would have been useful to indicate which line makes the error: I cannot run the code as I don't have JMyron.
BTW, that's not a compilation error but a runtime one. It might be hard to tell in Processing which compile and run in the same go.

The probable culprit is the line:

grid[r][c].z = int(dist(p1.x,p1.y,p2.x,p2.y));

particularly the part grid[r][c].z: you declare grid as an array of points, but if you don't put points (objects) there, each entry is null, hence the null pointer exception: null has no fields.

Looking further, I see you have the initGrid() method, but you don't call it... Smiley
Re: Syntax error when compiling
Reply #2 - Jun 6th, 2009, 1:50am
 
Altough you have created an array of Point references (grid) you need to create the actaul points by calling your initGrid() method from setup().
Page Index Toggle Pages: 1