0

Im using append on an empty array to create multiple projectiles, but every time I hit the spacebar Im getting a NullPointerException on keyPressed when it checks if the spacebar was pressed. I can't figure out why this is hapenning. Help would be really good.

MAIN CLASS:

int naveX, naveY;
int inimigoX, inimigoY;
Tiro [] tiros;

boolean tiroVisivel;
boolean naveDireita, naveEsquerda;

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

  naveX = 375; naveY = height - 120;
  inimigoX = 385; inimigoY = 0;

  tiroVisivel = false;
  naveDireita = naveEsquerda = false; 

}

void draw(){
  background (0);

  nave();
  movimentoNave();
  if (tiroVisivel == true && tiros != null){
    for (int i = 0; i < tiros.length; i++){
      tiros[i].display();
      tiros[i].update();      
    }
  }

  inimigo();
}

void nave(){
  stroke (255);
  fill (255, 0, 0);
  rect (naveX, naveY, 50, 100);
}

void movimentoNave(){
  if (naveEsquerda == true && naveX > 0){
    naveX = naveX - 5;
  }
  if (naveDireita == true && naveX < width - 50){
    naveX = naveX + 5;
  }
}

void inimigo(){
  stroke (255);
  fill (0, 100, 255);
  rect (inimigoX, inimigoY, 30, 50);
}

void keyPressed(){
  if (key == ' '){
    append(tiros, new Tiro(naveX + 20, naveY - 30));
    tiroVisivel = true;
  }

  if(key == 'a' || key == 'A'){
    naveEsquerda = true;  
  }
  if(key == 'd' || key == 'D'){
    naveDireita = true;
  }

  if (key == 'r' || key == 'R'){
    setup();    
  }
}

void keyReleased(){
  if(key == 'a' || key == 'A'){
    naveEsquerda = false;  
  }
  if(key == 'd' || key == 'D'){
    naveDireita = false;
  }    
}

PROJECTILE CLASS:

public class Tiro {

  private int tiroX = naveX + 20;
  private int tiroY = naveY - 30;

  public Tiro(int x, int y){
    this.tiroX = x;
    this.tiroY = y;
  }


  void display(){
    stroke (255);
    fill (255, 0, 100);
    rect (tiroX, tiroY, 10, 20); 
  }

  void update(){
    if (tiroY > 0){
      tiroY = tiroY - 10;
    }
  }
}
  • 1
    Might be key as I can't see that defined anywhere? – Alex Mar 20 '17 at 19:44
  • 2
    you never initialize the array *tiros*, so it create the *NullPointerException* when you try to access it's properties. You need to assign an initial value. – Mario Santini Mar 20 '17 at 19:46
  • the method `append` is not declared anywhere as well. Without a stacktrace and a correlation between the lines in the stacktrace to your code - it's impossible to help you. Please post you code in a way that we can reproduce the issue! – Nir Alfasi Mar 20 '17 at 19:48
  • @johncliffe and alfasin: they are part of [Tag:processing] though, see [keyPressed](https://processing.org/reference/keyPressed_.html) and [append](https://processing.org/reference/append_.html) – domsson Mar 20 '17 at 19:49

1 Answers1

0

Im using append on an empty array [...]

No, you are not. Your array isn't empty, it is uninitialized and therefore null.
That's because you declared, but never initialized your tiros array:

Tiro [] tiros;

Trying to access tiros as is will yield a NullPointerException.
To fix the problem, add this line in setup():

tiros = new Tiro[0];

And allow me this unrelated advice: if you can (and it looks as if you do), try to code in English. It is standard in the industry and will also benefit you in situations as these, when you seek help from an international community.

Community
  • 1
  • 1
domsson
  • 4,553
  • 2
  • 22
  • 40