# NullPointerException

**URL:** https://discourse.processing.org/t/nullpointerexception/5079
**Category:** Coding Questions
**Created:** [November 1, 2018, 2:38am UTC](https://discourse.processing.org/t/nullpointerexception/5079 "2018-11-01T02:38:18Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 2:38am UTC](https://discourse.processing.org/t/nullpointerexception/5079/1 "2018-11-01T02:38:18Z")

</div>

when i run my program, the message error say NullPointed Exception. could someone help me to fix my program? I’m still beginer with Processing

```auto
Benda[] bola = new Benda[20];
color warna;
PVector gravitasi;
PVector angin;
PVector gesek;
float total_gravitasi = 10;
float total_angin = 0;

void setup() {
  size(600, 400);
}  

void mousePressed() {
  if ( mouseButton == LEFT ) {
    for (int i=0; i < bola.length; i++) {
      strokeWeight(2);
      warna = color(random(255), random(255), random(255));
      bola[i] = new Benda(mouseX, mouseY, random(30), warna);
    }
  }
}

void keyPressed() {
  if (keyCode == UP) { 
    total_gravitasi+=0.5;
  } 
  if (keyCode == DOWN) { 
    total_gravitasi-=0.5;
  } 
  if (keyCode == LEFT) { 
    total_angin+=0.5;
  } 
  if (keyCode == RIGHT) { 
    total_angin-=0.5;
  }
}

void draw() {
  background(#ffffff);

  gravitasi = new PVector(0, total_gravitasi);
  angin = new PVector(total_angin, 0);
  gesek = new PVector(0, 0); 
  for (int i=0; i < bola.length; i++) {
    gesek = bola[i].kecepatan.copy();
    gesek.normalize();
    gesek.mult(-1);
    gesek.mult(0.5);  

    bola[i].menabrak(bola, i);
    bola[i].beriGaya(gesek);
    bola[i].beriGaya(gravitasi);
    bola[i].beriGaya(angin);
    bola[i].perbarui();
    bola[i].memantul();
    bola[i].tampilkan();
  }

  String str1 = "Gravitasi";
  String str2 = "Angin";
  println(str1, total_gravitasi);
  println(str2, total_angin);

  fill(0);
  textSize(20);
  text("Gravitasi :", 10, 50, total_gravitasi);
  text(total_gravitasi, 120, 50); 
  text("Angin :", 10, 70, total_angin);
  text(total_angin, 120, 70);
}

```

---

<div class="post-metadata">

### Author: ![Quillbert1](https://avatars.discourse-cdn.com/v4/letter/q/f475e1/32.png) [@Quillbert1](https://discourse.processing.org/u/Quillbert1)
#### Post date: [November 1, 2018, 4:03am UTC](https://discourse.processing.org/t/nullpointerexception/5079/2 "2018-11-01T04:03:57Z")

</div>

You have to create a class named Benda for this code to work

---

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 4:07am UTC](https://discourse.processing.org/t/nullpointerexception/5079/3 "2018-11-01T04:07:09Z")

</div>

```auto
class Benda {
  float massa;
  PVector lokasi;
  PVector kecepatan;
  PVector percepatan;
  Benda[] bolaLain;
  color warnaBola;

  Benda(float x, float y, float size, color warna){
    lokasi = new PVector(x,y);
    kecepatan = new PVector(random(-1,1),random(-1,1));
    percepatan = new PVector(0,0);
    massa = size;
    warnaBola = warna;
  }
  
  void tampilkan(){
    fill(warnaBola);
    ellipse(lokasi.x, lokasi.y, massa, massa);
  }
  
  void beriGaya(PVector gaya){
    PVector F = gaya.copy();
    F.div(massa);
    percepatan.add(F);
  }
  
  void perbarui(){
    kecepatan = kecepatan.add(percepatan);
    lokasi = lokasi.add(kecepatan);
    percepatan.mult(0);
  }
  
  void menabrak(Benda[] bola, int index){
    bolaLain = bola;
    float jarak, jarakMin, sudut;
    float arahX, arahY;
    for(int i=index+1;i< bola.length;i++){
       jarak = dist(bolaLain[i].lokasi.x, bolaLain[i].lokasi.y,
                    bola[index].lokasi.x, bola[index].lokasi.y );
       jarakMin = bolaLain[i].massa/2 + bola[index].massa/2;
       if( jarak < jarakMin ){
          sudut = atan2(bolaLain[i].lokasi.y - bola[index].lokasi.y,
                        bolaLain[i].lokasi.x - bola[index].lokasi.x);
          arahX = bola[index].lokasi.x + cos(sudut) * jarakMin;
          arahY = bola[index].lokasi.y + sin(sudut) * jarakMin;
          bola[index].percepatan.x = (arahX - bolaLain[i].lokasi.x) * 0.05;
          bola[index].percepatan.y = (arahY - bolaLain[i].lokasi.y) * 0.05;
          bola[index].kecepatan.x -= bola[index].percepatan.x;
          bola[index].kecepatan.y -= bola[index].percepatan.y;
          bolaLain[i].kecepatan.x += bola[index].percepatan.x;
          bolaLain[i].kecepatan.y += bola[index].percepatan.y;
       }
    }
  }
  
  void memantul(){
    if( lokasi.x > width-massa/2 ){
      kecepatan.x = kecepatan.x * -1;
    }
    if( lokasi.x < massa/2 ){
      kecepatan.x = kecepatan.x * -1;
    }
    if( lokasi.y > height-massa/2 ){
      lokasi.y = height-massa/2;
      kecepatan.y = kecepatan.y * -1;
    }
    if( lokasi.y < massa/2 ){
      kecepatan.y = kecepatan.y * -1;
    }
  }
}

```

there is class Benda , i forget to put this in my thread

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 1, 2018, 4:07am UTC](https://discourse.processing.org/t/nullpointerexception/5079/4 "2018-11-01T04:07:19Z")

</div>

You need to initialize your Bola class before calling it within draw. I assume you already have a bola class… You are currently initializing it the first time after you press the key, but you already try to access it in draw, which is started immeadetly after setup, and so you have no time to press any key.

---

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 4:08am UTC](https://discourse.processing.org/t/nullpointerexception/5079/5 "2018-11-01T04:08:45Z")

</div>

how to initialize my Bola class before calling it within draw?

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 1, 2018, 4:10am UTC](https://discourse.processing.org/t/nullpointerexception/5079/6 "2018-11-01T04:10:13Z")

</div>

Just add this inside setup, after size();

```auto
for (int i=0; i < bola.length; i++) {
      strokeWeight(2);
      warna = color(random(255), random(255), random(255));
      bola[i] = new Benda(mouseX, mouseY, random(30), warna);
    }

```

It’s the same code you have in mousePressed  
Just tried it, and it works.

---

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 4:20am UTC](https://discourse.processing.org/t/nullpointerexception/5079/7 "2018-11-01T04:20:36Z")

</div>

after i put this code inside setup

```auto
for (int i=0; i < bola.length; i++) {
      strokeWeight(2);
      warna = color(random(255), random(255), random(255));
      bola[i] = new Benda(mouseX, mouseY, random(30), warna);
    }

```

there is a ball that comes before I click the mouse.  
therefore, I deleted this code.

how to when i run my program is blank? then when i click my mouse, is start function mousePressed()

---

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 4:22am UTC](https://discourse.processing.org/t/nullpointerexception/5079/8 "2018-11-01T04:22:14Z")

</div>

i want to create program like this

```auto
ArrayList<Circle> circles = new ArrayList<Circle>();

void setup() {
  size(300, 300);
}

void mousePressed(){
  for(int i = 0; i < 100; i++){
    circles.add(new Circle(mouseX, mouseY));   
  }
}

void draw() {
  background(200);

  for (int i = circles.size()-1; i >= 0; i--) {
    circles.get(i).move();
    circles.get(i).display();
    
    if(circles.get(i).isOffScreen()){
      circles.remove(i);
    }
  }
}

class Circle {

  float x;
  float y;
  float xSpeed = random(-3, 3);
  float ySpeed = random(-3, 3);
  
  Circle(float x, float y){
    this.x = x;
    this.y = y;
  }
 
  void move() {
    x += xSpeed;
    y += ySpeed;
  }

  void display() {
    ellipse(x, y, 20, 20);
  }
  
  boolean isOffScreen(){
    return x < 0 || x > width || y < 0 || y > height;
  }
}

```

and then I want to added gravity and wind simulation at this program

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 1, 2018, 4:23am UTC](https://discourse.processing.org/t/nullpointerexception/5079/9 "2018-11-01T04:23:46Z")

</div>

Just add a boolean firstMousePressed = false; at the top and then encapsulate all of draw() inside it. Like:

```auto
void draw() {
  if (firstMousePressed) {
    //stuff inside draw
  }
}

```

---

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 4:31am UTC](https://discourse.processing.org/t/nullpointerexception/5079/10 "2018-11-01T04:31:06Z")

</div>

after encapsulate all of draw() to firstmousePressed, i get a new error in my class Benda at PVector F = gaya.copy() is NullPointerException

```auto
  void beriGaya(PVector gaya){
    PVector F = gaya.copy();
    F.div(massa);
    percepatan.add(F);
  }

```

---

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 4:35am UTC](https://discourse.processing.org/t/nullpointerexception/5079/11 "2018-11-01T04:35:07Z")

</div>

there is my class Benda

```auto
class Benda {
  float massa;
  PVector lokasi;
  PVector kecepatan;
  PVector percepatan;
  Benda[] bolaLain;
  color warnaBola;

  Benda(float x, float y, float size, color warna){
    lokasi = new PVector(x,y);
    kecepatan = new PVector(random(-1,1),random(-1,1));
    percepatan = new PVector(0,0);
    massa = size;
    warnaBola = warna;
  }
  
  void tampilkan(){
    fill(warnaBola);
    ellipse(lokasi.x, lokasi.y, massa, massa);
  }
  
  void beriGaya(PVector gaya){
    PVector F = gaya.copy();
    F.div(massa);
    percepatan.add(F);
  }
  
  void perbarui(){
    kecepatan = kecepatan.add(percepatan);
    lokasi = lokasi.add(kecepatan);
    percepatan.mult(0);
  }
  
  void menabrak(Benda[] bola, int index){
    bolaLain = bola;
    float jarak, jarakMin, sudut;
    float arahX, arahY;
    for(int i=index+1;i< bola.length;i++){
       jarak = dist(bolaLain[i].lokasi.x, bolaLain[i].lokasi.y,
                    bola[index].lokasi.x, bola[index].lokasi.y );
       jarakMin = bolaLain[i].massa/2 + bola[index].massa/2;
       if( jarak < jarakMin ){
          sudut = atan2(bolaLain[i].lokasi.y - bola[index].lokasi.y,
                        bolaLain[i].lokasi.x - bola[index].lokasi.x);
          arahX = bola[index].lokasi.x + cos(sudut) * jarakMin;
          arahY = bola[index].lokasi.y + sin(sudut) * jarakMin;
          bola[index].percepatan.x = (arahX - bolaLain[i].lokasi.x) * 0.05;
          bola[index].percepatan.y = (arahY - bolaLain[i].lokasi.y) * 0.05;
          bola[index].kecepatan.x -= bola[index].percepatan.x;
          bola[index].kecepatan.y -= bola[index].percepatan.y;
          bolaLain[i].kecepatan.x += bola[index].percepatan.x;
          bolaLain[i].kecepatan.y += bola[index].percepatan.y;
       }
    }
  }
  
  void memantul(){
    if( lokasi.x > width-massa/2 ){
      kecepatan.x = kecepatan.x * -1;
    }
    if( lokasi.x < massa/2 ){
      kecepatan.x = kecepatan.x * -1;
    }
    if( lokasi.y > height-massa/2 ){
      lokasi.y = height-massa/2;
      kecepatan.y = kecepatan.y * -1;
    }
    if( lokasi.y < massa/2 ){
      kecepatan.y = kecepatan.y * -1;
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 1, 2018, 4:36am UTC](https://discourse.processing.org/t/nullpointerexception/5079/12 "2018-11-01T04:36:12Z")

</div>

First of all, please change size(600,400) to size(600, 400, P3D); cause it cause a small notice to come up, but nothing big. And how did you get that error? I didn’t get it… Are you sure you only encapsulated the code inside draw and not also draw? Also, not more than the code inside?

---

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 4:39am UTC](https://discourse.processing.org/t/nullpointerexception/5079/13 "2018-11-01T04:39:14Z")

</div>

this is an example you gave

```auto
  if (firstMousePressed) {
    
    background(#ffffff);
   
    gravitasi = new PVector(0, total_gravitasi);
    angin = new PVector(total_angin, 0);
    gesek = new PVector(0, 0);

    for (int i=0; i < bola.length; i++) {
      gesek = bola[i].kecepatan.copy();
      gesek.normalize();
      gesek.mult(-1);
      gesek.mult(0.5);  

      bola[i].menabrak(bola, i);
      bola[i].beriGaya(gesek);
      bola[i].beriGaya(gravitasi);
      bola[i].beriGaya(angin);
      bola[i].perbarui();
      bola[i].memantul();
      bola[i].tampilkan();
    }
  }

```

is this code the way you want it to be?

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 1, 2018, 4:40am UTC](https://discourse.processing.org/t/nullpointerexception/5079/14 "2018-11-01T04:40:00Z")

</div>

Well, the whole thing should be like this :

```auto
Benda[] bola = new Benda[20];
color warna;
PVector gravitasi;
PVector angin;
PVector gesek;
float total_gravitasi = 10;
float total_angin = 0;

boolean firstMousePress = false;

void setup() {
  size(600, 400, P3D);
  for (int i=0; i < bola.length; i++) {
    strokeWeight(2);
    warna = color(random(255), random(255), random(255));
    bola[i] = new Benda(mouseX, mouseY, random(30), warna);
  }
}  

void mousePressed() {
  if ( mouseButton == LEFT ) {
    for (int i=0; i < bola.length; i++) {
      strokeWeight(2);
      warna = color(random(255), random(255), random(255));
      bola[i] = new Benda(mouseX, mouseY, random(30), warna);
    }
    firstMousePress = true;
  }
}

void keyPressed() {
  if (keyCode == UP) { 
    total_gravitasi+=0.5;
  } 
  if (keyCode == DOWN) { 
    total_gravitasi-=0.5;
  } 
  if (keyCode == LEFT) { 
    total_angin+=0.5;
  } 
  if (keyCode == RIGHT) { 
    total_angin-=0.5;
  }
}

void draw() {
  background(#ffffff);
  if (firstMousePress) {
    gravitasi = new PVector(0, total_gravitasi);
    angin = new PVector(total_angin, 0);
    gesek = new PVector(0, 0); 
    for (int i=0; i < bola.length; i++) {
      gesek = bola[i].kecepatan.copy();
      gesek.normalize();
      gesek.mult(-1);
      gesek.mult(0.5);  

      bola[i].menabrak(bola, i);
      bola[i].beriGaya(gesek);
      bola[i].beriGaya(gravitasi);
      bola[i].beriGaya(angin);
      bola[i].perbarui();
      bola[i].memantul();
      bola[i].tampilkan();
    }

    String str1 = "Gravitasi";
    String str2 = "Angin";
    println(str1, total_gravitasi);
    println(str2, total_angin);

    fill(0);
    textSize(20);
    text("Gravitasi :", 10, 50, total_gravitasi);
    text(total_gravitasi, 120, 50); 
    text("Angin :", 10, 70, total_angin);
    text(total_angin, 120, 70);
  }
}
class Benda {
  float massa;
  PVector lokasi;
  PVector kecepatan;
  PVector percepatan;
  Benda[] bolaLain;
  color warnaBola;

  Benda(float x, float y, float size, color warna) {
    lokasi = new PVector(x, y);
    kecepatan = new PVector(random(-1, 1), random(-1, 1));
    percepatan = new PVector(0, 0);
    massa = size;
    warnaBola = warna;
  }

  void tampilkan() {
    fill(warnaBola);
    ellipse(lokasi.x, lokasi.y, massa, massa);
  }

  void beriGaya(PVector gaya) {
    PVector F = gaya.copy();
    F.div(massa);
    percepatan.add(F);
  }

  void perbarui() {
    kecepatan = kecepatan.add(percepatan);
    lokasi = lokasi.add(kecepatan);
    percepatan.mult(0);
  }

  void menabrak(Benda[] bola, int index) {
    bolaLain = bola;
    float jarak, jarakMin, sudut;
    float arahX, arahY;
    for (int i=index+1; i< bola.length; i++) {
      jarak = dist(bolaLain[i].lokasi.x, bolaLain[i].lokasi.y, 
        bola[index].lokasi.x, bola[index].lokasi.y );
      jarakMin = bolaLain[i].massa/2 + bola[index].massa/2;
      if ( jarak < jarakMin ) {
        sudut = atan2(bolaLain[i].lokasi.y - bola[index].lokasi.y, 
          bolaLain[i].lokasi.x - bola[index].lokasi.x);
        arahX = bola[index].lokasi.x + cos(sudut) * jarakMin;
        arahY = bola[index].lokasi.y + sin(sudut) * jarakMin;
        bola[index].percepatan.x = (arahX - bolaLain[i].lokasi.x) * 0.05;
        bola[index].percepatan.y = (arahY - bolaLain[i].lokasi.y) * 0.05;
        bola[index].kecepatan.x -= bola[index].percepatan.x;
        bola[index].kecepatan.y -= bola[index].percepatan.y;
        bolaLain[i].kecepatan.x += bola[index].percepatan.x;
        bolaLain[i].kecepatan.y += bola[index].percepatan.y;
      }
    }
  }

  void memantul() {
    if ( lokasi.x > width-massa/2 ) {
      kecepatan.x = kecepatan.x * -1;
    }
    if ( lokasi.x < massa/2 ) {
      kecepatan.x = kecepatan.x * -1;
    }
    if ( lokasi.y > height-massa/2 ) {
      lokasi.y = height-massa/2;
      kecepatan.y = kecepatan.y * -1;
    }
    if ( lokasi.y < massa/2 ) {
      kecepatan.y = kecepatan.y * -1;
    }
  }
}

```

Although i left the background() outside, there shouldn’t be a difference…

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 1, 2018, 4:44am UTC](https://discourse.processing.org/t/nullpointerexception/5079/15 "2018-11-01T04:44:47Z")

</div>

And btw, when you are using BolaLain, you are making a copy of the global Bola[]. This is not needed, as it will cause you code to set a reference to the Bola[] for each Bola in the Array. This will essentially cause there to be instead of, if Bola[] size is 20, to be 20^2, which is 400… You can avoid this, by simply changing every BolaLain[i] to a normal reference to Bola[i] and removng the BolaLain[] Array from your class.  
You can do so quickly by pressing Str+F (Ctrl+F) and putting bolaLain in the top and bola in the bottom and then pressing on search and replace.

---

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 4:47am UTC](https://discourse.processing.org/t/nullpointerexception/5079/16 "2018-11-01T04:47:16Z")

</div>

Thank you for you help Lexyth…

and one more, how when I click the mouse, the previous ball doesn’t disappear ?

like this code

```auto
ArrayList<Circle> circles = new ArrayList<Circle>();

void setup() {
  size(300, 300);
}

void mousePressed(){
  for(int i = 0; i < 100; i++){
    circles.add(new Circle(mouseX, mouseY));   
  }
}

void draw() {
  background(200);

  for (int i = circles.size()-1; i >= 0; i--) {
    circles.get(i).move();
    circles.get(i).display();
    
    if(circles.get(i).isOffScreen()){
      circles.remove(i);
    }
  }
}

class Circle {

  float x;
  float y;
  float xSpeed = random(-3, 3);
  float ySpeed = random(-3, 3);
  
  Circle(float x, float y){
    this.x = x;
    this.y = y;
  }
 
  void move() {
    x += xSpeed;
    y += ySpeed;
  }

  void display() {
    ellipse(x, y, 20, 20);
  }
  
  boolean isOffScreen(){
    return x < 0 || x > width || y < 0 || y > height;
  }
}

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 1, 2018, 4:52am UTC](https://discourse.processing.org/t/nullpointerexception/5079/17 "2018-11-01T04:52:32Z")

</div>

Please recheck my previous post, if you havent, because i edited in a part about bolaLain. And to add a ball each time you press the mouse, you’ll have to change the code again and inside the mousePressed so that it too is encapsulated by firstMousePressed, but set to false. So :

```auto
void mousePressed () {
  if (!firstMousePressed) { 
    //stuff inside mousePressed
  }

  //and add a new part of what to do after the first mousePress
//in this case, adding a bola to the bola array.
}

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 1, 2018, 5:02am UTC](https://discourse.processing.org/t/nullpointerexception/5079/18 "2018-11-01T05:02:16Z")

</div>

I’ll add how it should look, so you can check if you got problems, but try to solve it yourself first. It’s always better to have more exercise, than less 😉

[SPOILER] Press on the Blurred field to show the Code, if you need help.

```auto
void mousePressed() {
  if ( mouseButton == LEFT ) {
    if (!firstMousePress) {
      for (int i=0; i < bola.length; i++) {
        strokeWeight(2);
        warna = color(random(255), random(255), random(255));
        bola[i] = new Benda(mouseX, mouseY, random(30), warna);
      }
      firstMousePress = true;
    } else {
      Benda[] bolaTemp = new Benda[bola.length + 1];
      for (int i = 0; i < bola.length; i++) {
       bolaTemp[i] = bola[i]; 
      }
      bolaTemp[bolaTemp.length-1] = new Benda(mouseX, mouseY, random(30), warna);
      bola = bolaTemp;
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 6:49am UTC](https://discourse.processing.org/t/nullpointerexception/5079/19 "2018-11-01T06:49:45Z")

</div>

thanks for u helping… 😀

god bless you

---

<div class="post-metadata">

### Author: ![rohmat](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rohmat](https://discourse.processing.org/u/rohmat)
#### Post date: [November 1, 2018, 8:14am UTC](https://discourse.processing.org/t/nullpointerexception/5079/20 "2018-11-01T08:14:07Z")

</div>

i’m sorry if i ask lot…  
how to make lots of balls like I clicked the mouse for the first time ?

there is my code…

```auto
Benda[] bola = new Benda[20];
color warna;
PVector gravitasi;
PVector angin;
PVector gesek;
float total_gravitasi = 10;
float total_angin = 0;

boolean firstMousePress = false;

void setup() {
  size(600, 400);
}  

void mousePressed() {
  if ( mouseButton == LEFT ) {
    if (!firstMousePress) {
        for (int i=0; i < bola.length; i++) {
        strokeWeight(2);
        warna = color(random(255), random(255), random(255));
        bola[i] = new Benda(mouseX, mouseY, random(30), warna);
      }
      firstMousePress = true;
    } 
    else {
      Benda[] bolaTemp = new Benda[bola.length + 1];
      for (int i = 0; i < bola.length; i++) {
        bolaTemp[i] = bola[i];
      }
      warna = color(random(255), random(255), random(255));
      bolaTemp[bolaTemp.length-1] = new Benda(mouseX, mouseY, random(30), warna);
      bola = bolaTemp;
    }
  }
}

void keyPressed() {
  if (keyCode == UP) { 
    total_gravitasi+=0.5;
  } 
  if (keyCode == DOWN) { 
    total_gravitasi-=0.5;
  } 
  if (keyCode == LEFT) { 
    total_angin+=0.5;
  } 
  if (keyCode == RIGHT) { 
    total_angin-=0.5;
  }
}

void draw() {
  background(255);
  if (firstMousePress) {
    gravitasi = new PVector(0, total_gravitasi);
    angin = new PVector(total_angin, 0);
    gesek = new PVector(0, 0); 
    for (int i=0; i < bola.length; i++) {
      gesek = bola[i].kecepatan.copy();
      gesek.normalize();
      gesek.mult(-1);
      gesek.mult(0.5);  

      bola[i].menabrak(bola, i);
      bola[i].beriGaya(gesek);
      bola[i].beriGaya(gravitasi);
      bola[i].beriGaya(angin);
      bola[i].perbarui();
      bola[i].memantul();
      bola[i].tampilkan();
    }
  }
  String str1 = "Gravitasi";
  String str2 = "Angin";
  println(str1, total_gravitasi);
  println(str2, total_angin);

  fill(0);
  textSize(20);
  text("Gravitasi :", 10, 50, total_gravitasi);
  text(total_gravitasi, 120, 50); 
  text("Angin :", 10, 70, total_angin);
  text(total_angin, 120, 70);
}

```

[Next page](https://discourse.processing.org/t/nullpointerexception/5079.md?page=2)
