Why do i get NullPointerException and how to fix it

import fisica.*;

FBox boxButton;
FCircle circleButton;
FPoly polyButton;

FWorld world;

color buttonColor = #155AAD;
color hoverColor = #55AA11;
color bodyColor = #E6B800;
float pos = 0;
float angle = 0;  
float gx = 11;
float gy = 12;


void setup() {
  size(600, 600);
  smooth();

  Fisica.init(this);
  world.setGravity(gx, gy);
  world = new FWorld();
  world.setEdges();
  world.remove(world.left);
  world.remove(world.right);
  world.remove(world.top);
}

void draw() {
  rectMode(CENTER);
  fill(0);
  background(255);
  rect(pos, 0, 20, 60);
  pos = map(sin(angle), -1, 1, 40, 560);
  angle += 0.02;
  world.step();
  world.draw();
}

void mousePressed() {
  FBody pressed = world.getBody(mouseX, mouseY);
  if (pressed == boxButton) {
    FBox myBox = new FBox(60, 60);
    myBox.setPosition(pos, 25);
    myBox.setRotation(random(-0.2, 0.2));
    myBox.setVelocity(0, 100);
    myBox.setFillColor(bodyColor);
    myBox.setNoStroke();
    world.add(myBox);
  }
}

Which line is highlighted when you get the error?

  world.setGravity(gx, gy);
  world = new FWorld();

possibly this 2 lines are out of order?

3 Likes

i already corrected the bug but thanks

i corrected the bug already but thanks