"NullPointerException" after switching turns

So I am trying to make a menu for a game I am building, but when I use the keyPressed to switch turns and Init the Game I get this error message. Any Idea why? Thanks In Advance ;)

space [] Space = new space[800];

int turn=1; //1=menu,2=initgame,3=gameover


void setup()
{ 
size(800,800);
//
if (turn==2)
{
initstarField();
}
}

void draw()
{
background(0);
if (turn==1)
{
menu();
}
else if (turn==2)
{
starField();
}
}

void keyTyped()
{
  if (turn==1 && key=='a')
  turn=2;
}

void menu()
{
if (turn==1)
{
background(0,0,255);

}
}

void initGame()
{

}

void spaceShip()
{

}

void asteroids()
{

}

void distanceTracker()
{

}

void initstarField()
{
for (int i = 0; i < Space.length; i++)
{
Space[i]=new space();
}
}

void starField()
{
translate(width/2, height/2);
for (int i = 0; i < Space.length; i++)
{
Space[i].lightSpeed();
Space[i].create();
}
}


 // class

class space
{
  float x;
  float y;
  float z;
  float pz;

  space()
  {
    x =random(-width, width);
    y =random(-height, height);  
    z =random (width);
    pz=z;
  }

  void lightSpeed()
  {
    z=z-25;
    if (z<1)
    {
      z=width;
      x =random(-width, width);
      y =random(-height, height); 
      pz=z;
    }
  }

  void create()
  {
    //Stars
    float sx = map (x / z, 0, 1, 0, width);
    float sy = map (y / z, 0, 1, 0, height);
    float r= map(z, 0, width, 16, 0);
    fill(255);
    ellipse(sx, sy, r, r);
    //LightSpeed Illusion
    float px = map(x/pz, 0, 1, 0, width);
    float py = map(y/pz, 0, 1, 0, width);
    pz=z;
    stroke(255);
    line(px, py, sx, sy);
  }
}

Answers

Sign In or Register to comment.