NullPointerException help

When I go to run my code

String[] names = { "CyberTruck", "Ford F10", "Ranger", "Sierra", "Titan" };

int nt = names.length;
int nb = nt + 2;
int b1 = nt, b2=nt+1;

float track= 200, trackY = 150, trackDY=100;
float start=100, finish=800, horizon=track-50;
float xNews=350, yNews=horizon+25;


Truck[] trucks = new Truck[nt];
Track[] tracks = new Track[nt];
Button[] buttons = new Button[nb];

int leader, winner;

void setup() {

  size(1000, 700);
  finish= width-200;
  frameRate(60);
  objects();
  //reset();
}
void draw() { 
  scene();
  show();
}

void objects() {
  buttons[b2] = new Button( "All", start, 100, color(255));
  float y=trackY;
  for (int i=0; i<nt; i++) {
    tracks[i] = new Track( y );
    trucks[i] = new Truck(names[i], start, y+35);
    buttons[i] = new Button(names[i], start-100, y, trucks[i].c);
    y += trackDY;
  }
}

void scene() { 
  background( 200, 220, 255 );    // Blue sky
  fill( 200, 220, 200 );          // Green grass
  rect( 0, 100, width, height );
}
void show() {
  for (int i=0; i<nt; i++) {
    fill(100, 0, 0);
    tracks[i].show();
    trucks[i].show();
    buttons[i].show();
  }
  buttons[b1].show();
}
void showAll() {
}
class Track {
  float y;
  Track( float ynew ) { 
    y=ynew;
  }
  void show() {
    fill(255);
    rect( start, y, finish-start+100, 80 );
  }
}

class Button
{
  float x, y;
  float w=90, h=25;
  String name;
  color c=color(255);
  //
  // CONSTRUCTORS //
  Button() {
  };
  Button( String name, float x, float y, color c ) {
    this.name= name; 
    this.x= x; 
    this.y= y;
    this.c= c;
  }
  // METHODS //
  void show() {
    fill(c);
    rect( x, y, w, h );
    fill(0);
    text( name, x+5, y+15 );
  }
  boolean clicked() {
    return hit( mouseX, mouseY );
  }
  boolean hit( float x2, float y2) {

    if (x2 < x) return false;
    if (x2 > x+w) return false;
    if (y2 < y) return false;
    if (y2 > y+h) return false;
    return true;
  }
}
class Truck
{
  float x=start, y, w= 100, h=33;
  String name;
  color c=color(random(255), random(255), random(255));
  //
  // CONSTRUCTORS //
  Truck() {
  };
  Truck( String newname, float newx, float newy ) {
    this.name= newname; 
    this.x= newx; 
    this.y= newy;
  }
  // METHODS //
  void show() {
    fill(c);
    rect(x, y, w, h);
    fill(149, 149, 149);
    pushMatrix();
    translate(x+20, y+33);
    rotate(frameCount / 50.0);
    polygon(0, 0, 12.5, 15);
    popMatrix();
    pushMatrix();
    translate(x+75, y+33);
    rotate(frameCount / 50.0);
    polygon(0, 0, 12.5, 15);
    popMatrix();
    fill(c);
    // ellipse(x+20, y+33, 24, 24);
    // ellipse(x+75, y+33, 24, 24);
    strokeWeight(1);
    line(x+90, y, x+83, y-25);
    line(x+83, y-25, x+65, y-25);
    line(x+65, y-25, x+65, y);
    fill(0);
    text( name, x+5, y+15 );
  }

  void polygon(float x, float y, float radius, int npoints) {
    float angle = TWO_PI / npoints;
    beginShape();
    for (float a = 0; a < TWO_PI; a += angle) {
      float sx = x + cos(a) * radius;
      float sy = y + sin(a) * radius;
      vertex(sx, sy);
    }
    endShape(CLOSE);
  }
  void move() {
    // This Racer moves (a random amount) forward.
    x = x + random(5, 15);
  }
}
void mousePressed() {

  for (int i=0; i<nt; i++) {
    if (buttons[i].clicked()) trucks[i].move();
  }
}

On line 54

buttons[b1].show(); 

I get the error " NullPointerException "

Could someone explain what I’m doing wrong please and how to not

1 Like
  • You haven’t assigned a Button instance to buttons[] at index b1 yet.
  • You have for b2 though: buttons[b2] = new Button( "All", start, 100, color(255));
  • And all indices < nt: buttons[i] = new Button(names[i], start-100, y, trucks[i].c);
1 Like

wow i completely overlooked that, thank you so much!