Problems with converting a P5.js sketch to Processing

Hi, after reading some documentation I reached this stage but I’m getting an error


// A Mover object
Mover mover;

void setup() {
  size(640,360);
  mover = new Mover(); 
}

void draw() {
  background(0);
  
  // Update the location
  mover.update();
  // Display the Mover
  mover.display(); 
}
class Mover {

  // The Mover tracks location, velocity, and acceleration 
  PVector location;
  PVector velocity;
  PVector acceleration;
  // The Mover's maximum speed
  Particles[] particle = new Particles[10];
  float topspeed;
  int e;

  Mover() {
    // Start in the center
    location = new PVector(width/2,height/2);
    velocity = new PVector(0,0);
    topspeed = 5;
    
    for(e = 0; e<9; e++){
      particle[e] = new Particles();
  }}

  void update() {
    
    // Compute a vector that points from location to mouse
    PVector mouse = new PVector(mouseX,mouseY);
    PVector acceleration = PVector.sub(mouse,location);
    // Set magnitude of acceleration
    acceleration.setMag(0.2);
    
    // Velocity changes according to acceleration
    velocity.add(acceleration);
    // Limit the velocity by topspeed
    velocity.limit(topspeed);
    // Location changes by velocity
    location.add(velocity);
  }

  void display() {
    fill(255);
    ellipse(location.x, location.y, 4, 4);
    for(int i = 9; i>0; i -=1){
      particle[i].x = particle[i-1].x;
      particle[i].y = particle[i-1].y;
    }
    particle[0].x = location.x;
    particle[0].y = location.y;
  }
}
class Particles {
  float x,y;
  
  Particles() {
    init();
  }
  void init(){
    x = 0;
    y = 0;
  }
}

I get nuller pointer exception in line

   particle[i].x = particle[i-1].x;

This code is to generate the particle (the head and tail).

Gonna keep trying

I found the problem, in

    for(e = 0; e<9; e++){
      particle[e] = new Particles();
  }

Had to change the 9 for the list length and that’s it :slight_smile: !

2 Likes