The usual NullPointerException, this time with mouseClicked()
in
Programming Questions
•
3 years ago
I am getting a NullPointerException when I use a mouseClicked() to add a new object to an array. I think it's a scope problem or perhaps the mouseClicked() event is happening in the middle of something causing confusion. My sketch models some charged particles moving around in 2 dimensions. When you click, it is supposed to add a new particle of random charge polarity where you click. I can't really see what the problem is specifically, though. Perhaps it's obvious to someone around here.
Thanks
Thanks
- // Global Declarations.
- int backgroundColor = 200;
- int noOfParticles = 0;
- // Temporary value to hold the field strength.
- float tempFieldStrength;
- Particle[] particles;
- // PVector holding a temporary field.
- PVector tempFieldVector;
-
- // Setup Function.
- void setup() {
- size(640, 480);
- particles = new Particle[16];
- particles[0] = new Particle(10, -10, 300, 200);
- noOfParticles++;
- particles[1] = new Particle(10, -10, 400, 400);
- noOfParticles++;
- particles[2] = new Particle(10, 10, 500, 300);
- noOfParticles++;
- particles[3] = new Particle(10, 10, 100, 100);
- noOfParticles++;
- }
-
- // Main Program Loop.
- void draw() {
- background(backgroundColor);
- // Locate particles.
- for (int i = 0; i < noOfParticles; i++) {
- particles[i].updateLocation(); // Here's where Processing highlights the crash...
- }
- // Draw particles.
- for (int i = 0; i < noOfParticles; i++) {
- particles[i].drawParticle();
- }
- // Update particle velocity.
- for (int i = 0; i < noOfParticles; i++) {
- particles[i].updateVelocity(i);
- }
- }
-
- void mouseClicked() {
- // Random charge
- float polarity = random(-1, 1);
- if (polarity > 0) polarity = 10;
- if (polarity < 0) polarity = -10;
- noOfParticles++;
- particles[noOfParticles+1] = new Particle(10, int(polarity), mouseX, mouseY);
- }
-
- class Particle {
- // Fields
- // The particle's mass.
- float mass;
- // The particle's charge.
- float charge;
- // The particle's physical size.
- int dia;
- // PVector representing particle location.
- PVector loc;
- // PVector representing particle velocity.
- PVector v;
- // Constructor
- Particle(float m, int c, int x, int y) {
- // How the constructor is called will determine the particle mass and charge.
- mass = m;
- charge = c;
- // The location of each new particle is chosen by the main program.
- loc = new PVector(x, y);
- // The velocity of all particles starts out at 0.
- v = new PVector(0, 0);
- tempFieldVector = new PVector(0, 0);
- }
- // Methods
- void drawParticle() {
- noStroke();
- // Particle fill color will be based on the charge.
- if (charge > 0) fill(255);
- if (charge < 0) fill(0);
- // Draw the particle at it's location vector.
- ellipse(loc.x, loc.y, int(mass), int(mass));
- }
- void updateVelocity(int whichOne) {
- // Velocity changes based on the pull of all the other particles.
- for (int i = 0; i < noOfParticles; i++) {
- if (whichOne != i) {
- // Add to the velocity vector the force due to each particle.
- tempFieldVector.x = loc.x - particles[i].loc.x;
- tempFieldVector.y = loc.y - particles[i].loc.y;
- tempFieldStrength = charge * particles[i].charge / sq(tempFieldVector.mag());
- tempFieldVector.normalize();
- tempFieldVector.mult(tempFieldStrength / mass);
- v.add(tempFieldVector);
- }
- }
- }
- void updateLocation() {
- // The location vector is based on the old location plus the velocity.
- loc.add(v);
- }
- }
1