# NullPointerException help

**URL:** <https://discourse.processing.org/t/nullpointerexception-help/21044>\
**Category:** Beginners\
**Created:** [May 19, 2020, 6:54am UTC](https://discourse.processing.org/t/nullpointerexception-help/21044 "2020-05-19T06:54:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dylan](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@Dylan](https://discourse.processing.org/u/Dylan)\
**Post date:** [May 19, 2020, 6:54am UTC](https://discourse.processing.org/t/nullpointerexception-help/21044/1 "2020-05-19T06:54:55Z")

</div>

When I go to run my code

```auto
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

```auto
buttons[b1].show(); 

```

I get the error " NullPointerException "

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 19, 2020, 7:12am UTC](https://discourse.processing.org/t/nullpointerexception-help/21044/2 "2020-05-19T07:12:45Z")

</div>

> [@Dylan](#):
>
> Could someone explain what I’m doing wrong…

- 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);`

---

<div class="post-metadata">

**Author:** ![Dylan](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@Dylan](https://discourse.processing.org/u/Dylan)\
**Post date:** [May 19, 2020, 7:14am UTC](https://discourse.processing.org/t/nullpointerexception-help/21044/3 "2020-05-19T07:14:56Z")

</div>

wow i completely overlooked that, thank you so much!
