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 › physics.makeAttraction LOOP ERROR
Page Index Toggle Pages: 1
physics.makeAttraction LOOP ERROR (Read 326 times)
physics.makeAttraction LOOP ERROR
Sep 30th, 2006, 6:33am
 
I get a nullPointer exception error when I run this script. it wasn't showing up until I added the loop that sets the attractions between particles. I marked this loop in the code. Is there something wrong with the loop or do i not understand something about the attractions and how the effect time? Any help would be great.


import traer.physics.*;

ParticleSystem physics;
Particle[][] particles;
Particle mouse;
int gridSize = 10;

void setup()
{
 size(800, 800);
 smooth();
 fill(0);
 framerate(60);
       
 physics = new ParticleSystem(0, 0,5,0.5);
 
 particles = new Particle[gridSize][gridSize];
 
 float gridStepX = (float) ((width / 2) / gridSize);
 float gridStepY = (float) ((height / 2) / gridSize);
 
 for (int i = 0; i < gridSize; i++)
 {
   for (int j = 0; j < gridSize; j++)
   {
     //Spring makeSpring( Particle a, Particle b, float strength, float damping, float restLength )
     particles[i][j] = physics.makeParticle(0.2, j * gridStepX + (width / 4), i * gridStepY + 30, sin(gridStepY + 30));
     if (j > 0)
     {
       physics.makeSpring(particles[i][j - 1], particles[i][j], 8.0, 0.5, cos(gridStepX)+20);
     }
   }
 }
 
 for (int j = 0; j < gridSize; j++)
 {
   for (int i = 1; i < gridSize; i++)
   {
     physics.makeSpring(particles[i - 1][j], particles[i][j], 8.0, 0.5,sin(gridStepY + cos(gridStepX*20)));
   }
 }
 
 
 
 
 //when I use this loop I get an error on the line that reads "physics.advanceTime(0.1);"
 
 for (int j = 0; j < gridSize; j++)
 {
  for (int i = 1; i < gridSize; i++)
  {
     physics.makeAttraction( mouse, particles[i][j], 1000, 10 );
     physics.makeAttraction( mouse, particles[i - 1][j], 1000, 10 );
   }
 }
 //end of loop
 
 
 
 
 particles[0][0].makeFixed();
 particles[0][gridSize - 1].makeFixed();
 particles[9][gridSize - 1].makeFixed();
 particles[9][gridSize - 10].makeFixed();
 mouse = physics.makeParticle();
 mouse.makeFixed();
}

void draw()
{

// when compiled I get a nullPointerException on this line
  physics.advanceTime(0.1);
  //end of null pointer exception
 
  mouse.moveTo( mouseX, mouseY, 0 );

 
 if (mousePressed)
 {
   particles[0][gridSize - 1].moveTo(mouseX, mouseY, 0);
   particles[0][gridSize - 1].velocity().clear();
 }
 
 background(255);
 
 for (int i = 0; i < gridSize; i++)
 {
   beginShape( LINE_STRIP );
   curveVertex(particles[i][0].position().x(), particles[i][0].position().y());
   for (int j = 0; j < gridSize; j++)
   {
     curveVertex(particles[i][j].position().x(), particles[i][j].position().y());
   }
   curveVertex(particles[i][gridSize - 1].position().x(), particles[i][gridSize - 1].position().y());
   endShape();
 }
 for (int j = 0; j < gridSize; j++)
 {
   beginShape( LINE_STRIP );
   curveVertex(particles[0][j].position().x(), particles[0][j].position().y());
   for (int i = 0; i < gridSize; i++)
   {
     curveVertex(particles[i][j].position().x(), particles[i][j].position().y());
   }
   curveVertex(particles[gridSize - 1][j].position().x(), particles[gridSize - 1][j].position().y());
   endShape();
 }
}

void mouseReleased()
{
 particles[0][gridSize - 1].setVelocity( (mouseX - pmouseX), (mouseY - pmouseY), 0 );
}
Page Index Toggle Pages: 1