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 › Why there is 'nullPointException' in my sketch
Page Index Toggle Pages: 1
Why there is 'nullPointException' in my sketch? (Read 1219 times)
Why there is 'nullPointException' in my sketch?
May 25th, 2010, 12:47am
 
I want to create a grid of polyhedron and then rotate them by interacting with mouse. However, the highlight part is what I want to rotate each polyhedron by its center.

Code:
int n = 24;
MyShape[] shape = new MyShape[n*n];
void setup(){
 size(750,750); //make the screen big enough to see
 for(int y=0; y<n; y++){ //for 12 steps in y
   for(int x=0; x<n; x++){ //for 12 steps in x
     //make a shape (calling the polygon constructor)
     shape[y*n+x] = new MyShape(3, 10.,x*20., y*20.);
     shape[y*n+x].move(10.*x, 10.*y);
     shape[y*n+x].move(30.,30.);
   }
 }
}
void draw(){
 background(255);
 for(int y=0; y<n; y++){ //for 12 steps in y
   for(int x=0; x<n; x++){ //for 12 steps in x
     shape[y*n+x].plot(); // plot the shapes
   }
 }
}

MyPoint[] ref = new MyPoint[n*n];
   
void mouseDragged(){

 for(int y=0; y<n; y++){ //for 12 steps in y
   for(int x=0; x<n; x++){ //for 12 steps in x

     ref[y*n+x].x = x;
     ref[y*n+x].y = y;
     
     //println(ref[y*n+x]);
     int xoff = mouseX - pmouseX;
     int yoff = mouseY - pmouseY;
     shape[y*n+x].rotate(xoff, ref[y*n+x]);
     shape[y*n+x].rotate(yoff, ref[y*n+x]);

   }
 }


}



class MyPoint {
 float x, y; // members of class
 //Constructor
 MyPoint(float xin, float yin){
   x = xin;
   y = yin;
 }
 //Move
 void move(float xoff, float yoff){
   x += xoff;
   y += yoff;
 }
 //Rotate
 void rotate(float angle, MyPoint ref){
   float cosa, sina;
   cosa = cos(radians(angle));
   sina = sin(radians(angle));
   float newx = (x-ref.x) * cosa - (y-ref.y) * sina + ref.x;
   float newy = (y-ref.y) * cosa + (x-ref.x) * sina + ref.y;
   x = newx;
   y = newy;
 }
 //Scale
 void scale(float xs, float ys, MyPoint ref){
   x = (x-ref.x)*xs + ref.x;
   y = (y-ref.y)*ys + ref.y;
 }
}


class MySegment {
 MyPoint start; // members of class
 MyPoint end;
 //Constructor
 MySegment(MyPoint p1, MyPoint p2){
   start = new MyPoint(p1.x, p1.y);
   end = new MyPoint(p2.x, p2.y);
 }
 //Move
 void move(float xoff, float yoff){
   start.move(xoff, yoff);
   end.move(xoff, yoff);
 }
 //Rotate
 void rotate (float angle, MyPoint ref) {
   start.rotate(angle, ref);
   end.rotate(angle, ref);
 }
 //Scale
 void scale(float xs, float ys, MyPoint ref){
   start.scale(xs, ys, ref);
   end.scale(xs, ys, ref);
 }
 // plot
 void plot (){
   line(start.x, start.y, end.x, end.y);
 }
}

class MyShape {
 MySegment[] segs; // members of class
 int numSegments;
 //Constructor
 MyShape(int numInputSegments, MySegment[] inputSegments){
   numSegments = numInputSegments;
   segs = new MySegment[numSegments];
   for(int i=0; i<numSegments; i++)
     segs[i] = inputSegments[i];
 }
 MyShape(int numSides, float radius, float xoff, float yoff){
   numSegments = numSides;
   segs = new MySegment[numSegments];
   // divide the full circle in nsides sections
   float angle = 2 * (float)Math.PI / numSegments;
   // create two points to store the segment points
   MyPoint p = new MyPoint(0.,0.);
   MyPoint pnext = new MyPoint(0.,0.);
   // loop to assign values to the points
   for(int i =0; i<numSegments; i++){
     p.x = xoff + radius * sin(angle*i);
     p.y = yoff + radius * cos(angle*i);
     pnext.x = xoff + radius * sin(angle*(i+1));
     pnext.y = yoff + radius * cos(angle*(i+1));
     segs[i] = new MySegment(p, pnext);
   }
 }
 // Move
 void move(float xoff, float yoff){
   for(int i=0; i<numSegments; i++)
     segs[i].move(xoff, yoff);
 }
 // Rotate
 void rotate (float angle, MyPoint ref) {
   for(int i=0; i<numSegments; i++)
     segs[i].rotate(angle, ref);
 }
 // Scale
 void scale(float xs, float ys, MyPoint ref){
   for(int i=0; i<numSegments; i++)
     segs[i].scale(xs, ys, ref);
 }
 // Plot
 void plot(){
   for(int i=0; i<numSegments; i++)
     segs[i].plot();
 }
}
Re: Why there is 'nullPointException' in my sketch?
Reply #1 - May 25th, 2010, 1:25am
 
You declare an empty array with:

 MyPoint[] ref = new MyPoint[n*n];

and then try and reference a non-existent element it in mouseDragged():

 ref[y*n+x].x = x;
 ref[y*n+x].y = y;

Replace the above two lines with:

 ref[y*n+x] = new MyPoint(x,y);

i.e. create a new element rather than reference an existing one...  Alternatively - and probably a little more efficient - do as you have with MyShape and first populate the ref array in setup.

BTW - not sure why you've created a MyPoint class that effectively does what PVector does - the rotate method could perhaps have been incorporated into the MyShape class.  In fact from a very quick scan it doesn't look like you use the MyPoint.rotate method

And to pre-empt your next question: Note how in the reference the rotate example first translates to the point around which to rotate.  Then have a look at pushMatrix
Re: Why there is 'nullPointException' in my sketch?
Reply #2 - May 25th, 2010, 8:27am
 
I did what you said and embed the new MyPoint array 'ref' in the setup(). I think I use the MyPoint's rotate function by passing from shape.rotate to segment.rotate and then finally to the point.rotate.

I still have a question that I thought these polygons should rotate by its own center and not sure why they seems group together and rotate by the upper corner of the screen.

Re: Why there is 'nullPointException' in my sketch?
Reply #3 - May 25th, 2010, 9:26am
 
archieboy wrote on May 25th, 2010, 8:27am:
I still have a question that I thought these polygons should rotate by its own center and not sure why they seems group together and rotate by the upper corner of the screen.


Quote:
And to pre-empt your next question: Note how in the reference the rotate example first translates to the point around which to rotate.  Then have a look at pushMatrix

Page Index Toggle Pages: 1