NULL POINTER EXCEPTION!!! PLEASE HELP
in
Programming Questions
•
1 year ago
I run this without the code to communicate with arduino and it works perfectly fine, but as soon as I tried to implement the code to communicate with arduino I ran into a lot of problems. this is just the latest, and I can't seem to work my way through it. Please Please help. The highlighted area is where Processing is telling me there is a NullPointerException. Thank you!
- //This code was inspired by Tom Carden's metropopDenim
- //Alexander Braden Feb 2012
- import processing.serial.*;
- int SPRITE_SIZE = 8;
- int NUM_PARTICLES = 10000;
- int NUM_ATTRACTORS = 7;
- float life = 200;
- Particle[] particle;
- Attractor[] attractor;
- Serial arduinoPort;
- String myString;
- void setup() {
- size(1500, 1000);
- background(0);
- smooth();
- arduinoPort = new Serial(this, Serial.list()[0], 9600);
- attractor = new Attractor[NUM_ATTRACTORS];
- particle = new Particle[NUM_PARTICLES];
- //scatter();
- // attractor[0] = new Attractor(934.49005, 755.15497);
- // attractor[1] = new Attractor(705.3572, 203.30354);
- // attractor[2] = new Attractor(691.54987, 729.0818);
- // attractor[3] = new Attractor(412.42975, 323.84244);
- // attractor[4] = new Attractor(518.8423, 395.56848);
- // attractor[5] = new Attractor(687.9407, 268.94116);
- }
- void draw() {
- if (lifeOut()) {
- // move and draw particles
- //fill(0, 2);
- // rect(0 , 0, width, height);
- stroke(255, 25);
- //stroke(random(245, 255), random(200, 215), random(0,10), 200); // use lower alpha for finer detail
- beginShape(POINTS);
- for (int i = 0; i < particle.length; i++) {
- particle[i].move();
- //vertex(particle[i].x,particle[i].y);
- //stroke(255, 2);
- line(particle[i].x, particle[i].y, particle[i].x+1, particle[i].y+1);
- }
- endShape();
- }
- else {
- fill(0, 5);
- rect(0, 0, width, height);
- }
- // Expand array size to the number of bytes you expect
- byte[] inBuffer = new byte[1];
- while (arduinoPort.available () > 0) {
- inBuffer = arduinoPort.readBytes();
- arduinoPort.readBytes(inBuffer);
- if (inBuffer != null) {
- myString = new String(inBuffer);
- //println(myString);
- }
- // reset on mouse click (now button from arduino)
- if (myString.charAt(0) == '1') {
- scatter();
- }
- }
- }
- void scatter() {
- life = 150;
- // randomise attractors
- for (int i = 0; i < attractor.length; i++) {
- attractor[i] = new Attractor();
- println("attractor["+i+"] = new Attractor("+attractor[i].x+","+attractor[i].y+"); "); // so you *can* get your favourite one back, if you want!
- }
- println();
- // randomise particles
- for (int i = 0; i < particle.length; i++) {
- particle[i] = new Particle();
- //stroke(random(0, 255), random(0, 100), random(0, 10), 200);
- //stroke(random(0, 255), random(0, 255), random(0,10), random(0, 100));
- }
- }
- boolean lifeOut() {
- life--;
- if (life < 0) {
- return false;
- }
- else {
- return true;
- }
- }
- class Attractor {
- float x,y;
- Attractor(float x, float y) {
- this.x = x;
- this.y = y;
- }
- Attractor() {
- // x = random(mouseX-300, mouseX+300);
- // y = random(mouseY-300, mouseY+300);
- x = random(width);
- y = random(height);
- }
- }
- float damp = 0.00002; // remember a very small amount of the last direction
- float accel = 20000.0; // move very quickly
- //float life = 255;
- class Particle {
- // location and velocity
- float x,y,z,vx,vy;
- Particle() {
- // initialise with random location:
- x = random(width);
- y = random(height);
- // initialise with random velocity:
- vx = random(-accel/2,accel/2);
- vy = random(-accel/2,accel/2);
- }
- void move() {
- // move towards every attractor
- // at a speed inversely proportional to distance squared
- // (much slower when further away, very fast when close)
- for (int i = 0; i < attractor.length; i++) {
- // calculate the square of the distance
- // from this particle to the current attractor
- float d2 = sq(attractor[i].x-x) + sq(attractor[i].y-y);
- if (d2 > 0.1) { // make sure we don't divide by zero
- // accelerate towards each attractor
- vx += accel * (attractor[i].x-x) / d2;
- vy += accel * (attractor[i].y-y) / d2;
- }
- }
- // move by the velocity
- x += vx;
- y += vy;
- // scale the velocity back for the next frame
- vx *= damp;
- vy *= damp;
- }
- boolean lightOut() {
- life--;
- if (life < 0) {
- return true;
- }
- else {
- return false;
- }
- }
- }
1