NullPointerException

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

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

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

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

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.

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

Just add this inside setup, after size();

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.

after i put this code inside setup

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()

i want to create program like this

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

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

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

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

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

there is my class Benda

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;
    }
  }
}

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?

this is an example you gave

  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?

Well, the whole thing should be like this :

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…

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.

Thank you for you help Lexyth…

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

like this code

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;
  }
}

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 :

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.
}

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 :wink:

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

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;
    }
  }
}

thanks for u helping… :grinning:

god bless you

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…

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