frankb
YaBB Newbies
Offline
Posts: 44
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 ); }