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 › What is a NullPointerException?
Page Index Toggle Pages: 1
What is a NullPointerException??? (Read 262 times)
What is a NullPointerException???
Sep 24th, 2006, 8:54pm
 
I keep getting this error message that reads NullPointerException. It started showing up when I added two for loops into the code. This is the entire script and bellow I have added a note at the part I added when the error showed up.




import traer.physics.*;

ParticleSystem physics;
Particle[][] particles;
Spring[][] springs;
int gridSize = 20;

void setup()
{
 size(800, 800);
 smooth();
 fill(0);
 framerate(60);
       
   physics = new ParticleSystem(0, 0,20,0.5);
 
 particles = new Particle[gridSize][gridSize];
 springs = new Spring[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 + 20, 0);
     if (j > 0)
     {
      springs[i][j] = physics.makeSpring(particles[i][j - 1], particles[i][j], 1.0, 0.5, sin(i)+30 );
     }
   }
 }
 
 for (int j = 0; j < gridSize; j++)
 {
   for (int i = 1; i < gridSize; i++)
   {
      physics.makeSpring(particles[i - 1][j], particles[i][j], 1, 0.5, 20);
   }
 }
 for (int a = 0; a < gridSize; a++)
{
 float f = random(20);
 int b = int(f);
 particles[a][0].makeFixed();
 particles[0][a].makeFixed();
 particles[gridSize-1][a].makeFixed();
 particles[a][gridSize-1].makeFixed();
 //add switch loop to stop if value exceeds gridsize then try to use complex sequence to fix points
 particles[a][a].makeFixed();

}
//array of spring tension/compression store info for all springs and color_code
 //convert_type


//This is where I get the error!!!!!!!!!!



   for (int y = 0; y < gridSize/2; y++)
{
 for (int t = 0; t < gridSize/2; t++)
{

int[] compressTest = new int[gridSize];

 float stress = springs[y][t].restLength();
 int stressInt = int(stress);
 println(stressInt);
 compressTest[t] = stressInt;
 println("spring rest length is equal to " + compressTest[t]);
 float compressTest2 = springs[1][1].currentLength();
 println("spring2 actual length is equal to " + compressTest2);
}
}

// end of the part I added that caused the error.

}

void draw()
{
 physics.advanceTime(0.1);
 
 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 );
}
Re: What is a NullPointerException???
Reply #1 - Sep 25th, 2006, 12:18pm
 
The problem is that you never create spring[0][0] but the rist time around
Code:
  for (int y = 0; y < gridSize/2; y++)
{
for (int t = 0; t < gridSize/2; t++)
{
int[] compressTest = new int[gridSize];
println("y="+y+" t="+t);
float stress = springs[y][t].restLength();
int stressInt = int(stress);
println(stressInt);
compressTest[t] = stressInt;
println("spring rest length is equal to " + compressTest[t]);
float compressTest2 = springs[1][1].currentLength();
println("spring2 actual length is equal to " + compressTest2);
}
}


the first spring you look at is spring[0][0], which doesn't exist, and so gives you the NullPointerException.

In a more general sense, if you get a NullPointerException it normally means you've named a variable (in this case spring[0][0], but not assigned it a value, and then try to use it as if it's been created.
Page Index Toggle Pages: 1