Issues using Classes - NullPointerException

edited February 2016 in Questions about Code

Cannot for the life of me get this working...I have to use classes and this is throwing up a Null Pointer exception, any help with the whole code and this would be appreciated

`class spaceship{ float PosX = width/2; //Sets the spaceship in the center float PosY = height/2; //Sets the spaceship in the center float PlayerSpeed = 0; //Creates a variable for the speed float PlayerSlow = 0.98; //Creates a variable for slowing the player float PlayerDirection = 0; //Creates a variable for the direction of the player float TurnSpeed = 3; //Creates a variable for the speed the player can turn float ShipScale=1; //Creates a variable for the size of the ship

PShape SpaceshipShape,ship,thruster1,thruster2,flame1,flame2; //Creates the shape classes to create the Spaceship

boolean die=false; //Creates a boolean for when the player dies boolean W=false; //Creates a boolean for the W key boolean A=false; //Creates a boolean for the A key boolean S=false; //Creates a boolean for the S key boolean D=false; //Creates a boolean for the D key

spaceship() { SpaceshipShape = createShape(GROUP); //Creates a group of shapes for the Spaceship

ship = createShape(); //Creates the shape ship ship.beginShape(); //Begins the shape ship.fill(230); //Sets the colour ship.stroke(0); //Sets the stroke colour ship.rotate(radians(90)); //Re-orientates the shape ship.vertex(25,0); //Sets a series of vertex to draw the main ship body ship.vertex(0,50); ship.vertex(25,35); ship.vertex(50,50); ship.vertex(25,0); ship.endShape(); //Ends the shape

//thruster1, thruster2, flame1 and flame2 use the same process as above

thruster1 = createShape(); thruster1.beginShape(); thruster1.fill(125); thruster1.stroke(0); thruster1.rotate(radians(90)); thruster1.vertex(15,29); thruster1.vertex(20,29); thruster1.vertex(20,50); thruster1.vertex(15,50); thruster1.endShape();

thruster2 = createShape(); thruster2.beginShape(); thruster2.fill(125); thruster2.stroke(0); thruster2.rotate(radians(90)); thruster2.vertex(30,29); thruster2.vertex(35,29); thruster2.vertex(35,50); thruster2.vertex(30,50); thruster2.endShape();

flame1 = createShape(); flame1.beginShape(); flame1.fill(255,0,0); flame1.stroke(0); flame1.rotate(radians(90)); flame1.vertex(15,50); flame1.vertex(20,50); flame1.vertex(17.5,58); flame1.endShape();

flame2 = createShape(); flame2.beginShape(); flame2.fill(255,0,0); flame2.stroke(0); flame2.rotate(radians(90)); flame2.vertex(30,50); flame2.vertex(35,50); flame2.vertex(32.5,58); flame2.endShape();

SpaceshipShape.addChild(ship); //Adds the ship shape to the Spaceship group SpaceshipShape.addChild(thruster1); //Adds the thruster1 shape to the Spaceship group SpaceshipShape.addChild(thruster2); //Adds the thruster 2 shape to the Spaceship group }`

Answers

  • please post entire code ASAP

  • you need before setup e.g.

    spaceship s1 = new spaceship();

  • spaceship s1; 
    
    void setup() {
      size(999, 999);
      println(GROUP);
      s1 = new spaceship();
       // ME=this;
    }
    
    void draw() {
    
      s1.show();
    }
    
    
    
    
    
    class spaceship { 
    
      float PosX = width/2; //Sets the spaceship in the center 
      float PosY = height/2; //Sets the spaceship in the center
      float PlayerSpeed = 0; //Creates a variable for the speed 
      float PlayerSlow = 0.98; //Creates a variable for slowing the player
      float PlayerDirection = 0; //Creates a variable for the direction of the player 
      float TurnSpeed = 3; //Creates a variable for the speed the player can turn 
      float ShipScale=1; //Creates a variable for the size of the ship
    
      PShape SpaceshipShape, ship, thruster1, thruster2, flame1, flame2; //Creates the shape classes to create the Spaceship
    
      boolean die=false; //Creates a boolean for when the player dies 
      boolean W=false; //Creates a boolean for the W key
      boolean A=false; //Creates a boolean for the A key 
      boolean S=false; //Creates a boolean for the S key
      boolean D=false; //Creates a boolean for the D key
    
      spaceship() { 
    
        ship = createShape(); //Creates the shape ship 
        ship.beginShape(); //Begins the shape
        ship.fill(230); //Sets the colour 
        ship.stroke(0); //Sets the stroke colour 
        ship.rotate(radians(90)); //Re-orientates the shape
        ship.vertex(25, 0); //Sets a series of vertex to draw the main ship body 
        ship.vertex(0, 50); 
        ship.vertex(25, 35); 
        ship.vertex(50, 50); 
        ship.vertex(25, 0); 
        ship.endShape(); //Ends the shape
    
        //thruster1, thruster2, flame1 and flame2 use the same process as above
        thruster1 = createShape(); 
        thruster1.beginShape(); 
        thruster1.fill(125); 
        thruster1.stroke(0); 
        thruster1.rotate(radians(90)); 
        thruster1.vertex(15, 29); 
        thruster1.vertex(20, 29); 
        thruster1.vertex(20, 50); 
        thruster1.vertex(15, 50); 
        thruster1.endShape();
    
        thruster2 = createShape(); 
        thruster2.beginShape(); 
        thruster2.fill(125); 
        thruster2.stroke(0); 
        thruster2.rotate(radians(90)); 
        thruster2.vertex(30, 29); 
        thruster2.vertex(35, 29); 
        thruster2.vertex(35, 50); 
        thruster2.vertex(30, 50); 
        thruster2.endShape();
    
        flame1 = createShape(); 
        flame1.beginShape(); 
        flame1.fill(255, 0, 0); 
        flame1.stroke(0); 
        flame1.rotate(radians(90)); 
        flame1.vertex(15, 50); 
        flame1.vertex(20, 50); 
        flame1.vertex(17.5, 58); 
        flame1.endShape();
    
        flame2 = createShape(); 
        flame2.beginShape(); 
        flame2.fill(color(255, 0, 0)); 
        flame2.stroke(0); 
        flame2.rotate(radians(90)); 
        flame2.vertex(30, 50); 
        flame2.vertex(35, 50); 
        flame2.vertex(32.5, 58); 
        flame2.endShape();
    
        SpaceshipShape = new PShape(); //createShape(ELLIPSE, -25, 0, 50, 50); //Creates a group of shapes for the Spaceship
        SpaceshipShape = createShape(GROUP);
        // SpaceshipShape.beginShape(); 
        // SpaceshipShape.beginShape(); //Begins the shape
        SpaceshipShape.addChild(ship); //Adds the ship shape to the Spaceship group
        SpaceshipShape.addChild(thruster1); //Adds the thruster1 shape to the Spaceship group 
        SpaceshipShape.addChild(thruster2); //Adds the thruster 2 shape to the Spaceship group
        SpaceshipShape.addChild(flame1); //Adds flame shape to the Spaceship group
        SpaceshipShape.addChild(flame2); //Adds flame 2 shape to the Spaceship group
        // SpaceshipShape.endShape();
      }
    
      void show() {
        shape(SpaceshipShape, 300, 333);
      }
    }
    
  • edited February 2016

    Hey here's the full code. It was in two different tabs but for the sake of this I've implemented your corrections into my code and moved it all into one big code, hopefully I can split it up later. This got rid of the NullPointerException error and I played around with it for a while but then just threw up another.....very confused

    ` spaceship s1;

    void setup() { size(800,800); s1 = new spaceship(); }

    class spaceship{ float PosX = width/2; //Sets the spaceship in the center float PosY = height/2; //Sets the spaceship in the center float PlayerSpeed = 0; //Creates a variable for the speed float PlayerSlow = 0.98; //Creates a variable for slowing the player float PlayerDirection = 0; //Creates a variable for the direction of the player float TurnSpeed = 3; //Creates a variable for the speed the player can turn float ShipScale=1; //Creates a variable for the size of the ship

    PShape SpaceshipShape,ship,thruster1,thruster2,flame1,flame2; //Creates the shape classes to create the Spaceship

    boolean die=false; //Creates a boolean for when the player dies boolean W=false; //Creates a boolean for the W key boolean A=false; //Creates a boolean for the A key boolean S=false; //Creates a boolean for the S key boolean D=false; //Creates a boolean for the D key

    spaceship() {

    ship = createShape(); //Creates the shape ship ship.beginShape(); //Begins the shape ship.fill(230); //Sets the colour ship.stroke(0); //Sets the stroke colour ship.rotate(radians(90)); //Re-orientates the shape ship.vertex(25,0); //Sets a series of vertex to draw the main ship body ship.vertex(0,50); ship.vertex(25,35); ship.vertex(50,50); ship.vertex(25,0); ship.endShape(); //Ends the shape

    //thruster1, thruster2, flame1 and flame2 use the same process as above

    thruster1 = createShape(); thruster1.beginShape(); thruster1.fill(125); thruster1.stroke(0); thruster1.rotate(radians(90)); thruster1.vertex(15,29); thruster1.vertex(20,29); thruster1.vertex(20,50); thruster1.vertex(15,50); thruster1.endShape();

    thruster2 = createShape(); thruster2.beginShape(); thruster2.fill(125); thruster2.stroke(0); thruster2.rotate(radians(90)); thruster2.vertex(30,29); thruster2.vertex(35,29); thruster2.vertex(35,50); thruster2.vertex(30,50); thruster2.endShape();

    flame1 = createShape(); flame1.beginShape(); flame1.fill(255,0,0); flame1.stroke(0); flame1.rotate(radians(90)); flame1.vertex(15,50); flame1.vertex(20,50); flame1.vertex(17.5,58); flame1.endShape();

    flame2 = createShape(); flame2.beginShape(); flame2.fill(255,0,0); flame2.stroke(0); flame2.rotate(radians(90)); flame2.vertex(30,50); flame2.vertex(35,50); flame2.vertex(32.5,58); flame2.endShape();

    SpaceshipShape = new PShape(); SpaceshipShape = createShape(GROUP); //SpaeceshipShape.beginShape; SpaceshipShape.addChild(ship); //Adds the ship shape to the Spaceship group SpaceshipShape.addChild(thruster1); //Adds the thruster1 shape to the Spaceship group SpaceshipShape.addChild(thruster2); //Adds the thruster 2 shape to the Spaceship group //SpaceshipShape.endShape; }

    void keyPressed() { if(key=='w') W=true; //If the w key is pressed, the W boolean is switched from false to true if(key=='a') A=true; //If the a key is pressed, the A boolean is switched from false to true if(key=='s') S=true; //If the s key is pressed, the S boolean is switched from false to true if(key=='d') D=true; //If the d key is pressed, the D boolean is switched from false to true }

    void keyReleased() { if(key=='w') W=false; //If the w key is released, the W boolean is switched from true to false if(key=='a') A=false; //If the a key is released, the A boolean is switched from true to false if(key=='s') S=false; //If the s key is released, the S boolean is switched from true to false if(key=='d') D=false; //If the d key is released, the D boolean is switched from true to false }

    void update() { if(W && die==false) { //If W is pressed and the player is alive PlayerSpeed += 0.1; //The speed is incremented TurnSpeed += 0.1; //The turn speed is incremented shape(flame1,0,0); //The flame shapes appear shape(flame2,0,0); }

    if(S) { PlayerSlow=0.9; //If S is pressed the slow speed is increased } else { PlayerSlow=0.98; //Otherwise it remains at 0.98 }

    if(W==false && die==false) {  //If W isn't pressed and the player is alive
    PlayerSpeed=PlayerSpeed*PlayerSlow;  //The player speed is slowed by multiplying it with the slowing speed
    

    }

    if(A && die==false) { //If A is pressed and the player is alive PlayerDirection -=radians(TurnSpeed); //The player rotates left according to the turn speed } if(D && die==false) { //If D is pressed and the player is alive PlayerDirection +=radians(TurnSpeed); //the player rotates right according to the turn speed }

    if(die) { //If the player is dead PlayerDirection+=0.3; //The player spins continously ShipScale-=0.01; //The spaceship gets smaller if(ShipScale<=0) { //If the spaceship scale is 0 ShipScale=0; //It remains set at 0 } } }

    void display() { translate(PosX,PosY); //Translates the ship to the PosX and PosY variables rotate(PlayerDirection); //Rotates the ship according to the direction variable scale(ShipScale); //Scales the ship with the scaling variable shape(SpaceshipShape,0,0); //Draws the shape Spaceship

    PosX+=cos(PlayerDirection)PlayerSpeed; //Calculates the X position acorrding to the players direction and speed PosY+=sin(PlayerDirection)PlayerSpeed; //Calculates the Y position acorrding to the players direction and speed }

    void show() { shape(SpaceshipShape, 300, 300); }

    void draw() { background(255);

    s1.show(); spaceship.update(); spaceship.display();

    } } `

  • edited February 2016

    go back, edit your post please

    there is a sticky post on how to format code in the post

    I think there is some kind of bug in version 3 with GROUP

  • spaceship s1;
    
    void setup() {
      size(800,800);
      s1 = new spaceship();
    }
    
    class spaceship{
      float PosX = width/2;  //Sets the spaceship in the center 
      float PosY = height/2;  //Sets the spaceship in the center
      float PlayerSpeed = 0;  //Creates a variable for the speed
      float PlayerSlow = 0.98;  //Creates a variable for slowing the player
      float PlayerDirection = 0;  //Creates a variable for the direction of the player
      float TurnSpeed = 3;  //Creates a variable for the speed the player can turn
      float ShipScale=1;  //Creates a variable for the size of the ship
    
      boolean die=false;  //Creates a boolean for when the player dies
      boolean W=false;  //Creates a boolean for the W key
      boolean A=false;  //Creates a boolean for the A key
      boolean S=false;  //Creates a boolean for the S key
      boolean D=false;  //Creates a boolean for the D key
    
      spaceship() {
    
      PShape SpaceshipShape,ship,thruster1,thruster2,flame1,flame2;  //Creates    the shape classes to create the Spaceship
    
      ship = createShape();  //Creates the shape ship
      ship.beginShape();  //Begins the shape
      ship.fill(230);  //Sets the colour
      ship.stroke(0);  //Sets the stroke colour
      ship.rotate(radians(90));  //Re-orientates the shape
      ship.vertex(25,0);  //Sets a series of vertex to draw the main ship body
      ship.vertex(0,50);
      ship.vertex(25,35);
      ship.vertex(50,50);
      ship.vertex(25,0);
     ship.endShape();  //Ends the shape
    

    //thruster1, thruster2, flame1 and flame2 use the same process as above

      thruster1 = createShape();
      thruster1.beginShape();
    
      thruster1.fill(125);
      thruster1.stroke(0);
      thruster1.rotate(radians(90));
      thruster1.vertex(15,29);
      thruster1.vertex(20,29);
      thruster1.vertex(20,50);
      thruster1.vertex(15,50);
      thruster1.endShape();
    
      thruster2 = createShape();
      thruster2.beginShape();
      thruster2.fill(125);
      thruster2.stroke(0);
      thruster2.rotate(radians(90));
      thruster2.vertex(30,29);
      thruster2.vertex(35,29);
      thruster2.vertex(35,50);
      thruster2.vertex(30,50);
      thruster2.endShape();
    
      flame1 = createShape();
      flame1.beginShape();
      flame1.fill(255,0,0);
      flame1.stroke(0);
      flame1.rotate(radians(90));
      flame1.vertex(15,50);
      flame1.vertex(20,50);
      flame1.vertex(17.5,58);
      flame1.endShape();
    
      flame2 = createShape();
      flame2.beginShape();
      flame2.fill(255,0,0);
      flame2.stroke(0);
      flame2.rotate(radians(90));
      flame2.vertex(30,50);
      flame2.vertex(35,50);
      flame2.vertex(32.5,58);
      flame2.endShape();
    
      SpaceshipShape = new PShape();
      SpaceshipShape = createShape(GROUP);
      //SpaeceshipShape.beginShape;
      SpaceshipShape.addChild(ship);  //Adds the ship shape to the Spaceship group
       SpaceshipShape.addChild(thruster1);  //Adds the thruster1 shape to the     Spaceship group
       SpaceshipShape.addChild(thruster2);  //Adds the thruster 2 shape to the Spaceship group
      //SpaceshipShape.endShape;
      }
    
    
       void keyPressed() {
       if(key=='w')
        W=true;   //If the w key is pressed, the W boolean is switched from false to   true
      if(key=='a')
       A=true;   //If the a key is pressed, the A boolean is switched from false to true
      if(key=='s')
        S=true;  //If the s key is pressed, the S boolean is switched from false to true
     if(key=='d')
        D=true;   //If the d key is pressed, the D boolean is switched from false to true
    }
    
    void keyReleased() {
      if(key=='w')
        W=false;  //If the w key is released, the W boolean is switched from true to false
      if(key=='a')
         A=false;   //If the a key is released, the A boolean is switched from true to false
      if(key=='s')
        S=false;   //If the s key is released, the S boolean is switched from true to false
      if(key=='d')
        D=false;   //If the d key is released, the D boolean is switched from true to false
     }
    
      void update() {
       if(W && die==false) {  //If W is pressed and the player is alive
        PlayerSpeed += 0.1;   //The speed is incremented
        TurnSpeed += 0.1;   //The turn speed is incremented
    //shape(flame1,0,0);  //The flame shapes appear
    //shape(flame2,0,0);
      =}
    
       if(S) {
        PlayerSlow=0.9;  //If S is pressed the slow speed is increased
      } else {
        PlayerSlow=0.98;  //Otherwise it remains at 0.98
      }
    
        if(W==false && die==false) {  //If W isn't pressed and the player is alive
        PlayerSpeed=PlayerSpeed*PlayerSlow;  //The player speed is slowed by   multiplying it with the slowing speed
      }
    
      if(A && die==false) {  //If A is pressed and the player is alive
          PlayerDirection -=radians(TurnSpeed);   //The player rotates left according to the turn speed
        }
        if(D && die==false) {  //If D is pressed and the player is alive
          PlayerDirection +=radians(TurnSpeed);   //the player rotates right according to the turn speed
       }
    
       if(die) {  //If the player is dead
           PlayerDirection+=0.3;   //The player spins continously
           ShipScale-=0.01;  //The spaceship gets smaller
           if(ShipScale<=0) {  //If the spaceship scale is 0
            ShipScale=0;  //It remains set at 0
           }
       }
       }
    
       void display() {
         translate(PosX,PosY);  //Translates the ship to the PosX and PosY variables
         rotate(PlayerDirection);  //Rotates the ship according to the direction variable
         scale(ShipScale);  //Scales the ship with the scaling variable
         shape(SpaceshipShape,0,0);  //Draws the shape Spaceship
    
       PosX+=cos(PlayerDirection)*PlayerSpeed;  //Calculates the X position acorrding to the players direction and speed
       PosY+=sin(PlayerDirection)*PlayerSpeed;  //Calculates the Y position acorrding to the players direction and speed
      }
    
       void show() {
         shape(SpaceshipShape, 300, 300);
       }
    
       void draw() {
      background(255);
    
       s1.show();
      spaceship.update();
      spaceship.display();
    
     }
     }
    
  • Answer ✓

    This must come before line 23

    PShape SpaceshipShape;

  • Ok that fixed that issue but now it comes up with

    'Cannot make a static reference to the non-static method update() from the type player_Game.spaceship'

    This is getting confusing now for me :S I saw on a forum post about using display and update and never used it before

  • Your error is presumably at line 128 of the last code section.

    You can declare a method as static and then you can call it without having an actual object of that class. The syntax for this is:

    void setup()
    {
      Foo.bar();            //We can call bar() of the Foo class without having an actual object of Foo
    }
    
    
    static class Foo
    {
      static void bar()
      {
        println("I'm a static method!");
      }
    }
    

    so if you do ClassName.staticMethod() you can call a static method without having an object (no need for Foo foo = new Foo() ). Since your class is named spaceship, Processing thinks update() needs to be a static method, but it isn't, so that's why you get the error.

    To get rid of the error simply call update() on an instance of the spaceship class (I guess s1? but am getting lost in your code).

    This error could probably have been avoided if you adhered to the syntax guidelines: use a capital letter for class names and use small letters for variables and objects.

    Also next time you post your code, hit ctrl+t in Processing first. It makes it much easier to spot syntax errors.

  • edited February 2016

    ... and use small letters for variables and objects.

    I believe you've meant variables & functions? Objects don't have a name aFaIK...
    P.S.: Perhaps we may say objects got a surname instead. That is, the class which they're based on. :D

  • I've annoyingly fixed the static issue a different way but now have another NullpointerException on line 114. It doesn't seem to like the shape created of SpaceshipShape and I can't figure out why as it's been initialized ...

  • I believe you've meant variables & functions? Objects don't have a name aFaIK...

    Yes of course, I shouldn't be helping people when I should be sleeping ;)

    @CVaughan, please post your code again (and format it first with ctrl+t in Processing). It looks like something screwed up somewhere. I'm unsure if void draw() belongs to the spaceship class or to the main sketch.

    Nullpointer errors are quite easy to solve once you get the hang of it. First try to identify what might be null. Try to identify suspects (even if you don't think they are null). Then test if they're null, for example like this with SpaceshipSpace: println(SpaceshipSpace == null); before the line where the null pointer error happens. If this prints true, then SpaceshipSpace is somehow null even though you think it shouldn't be. You can ignore this by doing something like if(SpaceshipSpace != null) shape(SpaceshipShape,0,0); (which is cheating!) or tracing the flow of your code upwards until you realise where you should have initialised SpaceshipShape instead.

  • Ok so I reformatted the code to make it simpler with different class systems, however still getting this NullPointer. I implement the if statement which you suggested, very clever, however that didn't work and I can't see where I should initialize the shape

     class spaceship {
      float PosX = width/2;  //Sets the spaceship in the center 
      float PosY = height/2;  //Sets the spaceship in the center
      float PlayerSpeed = 0;  //Creates a variable for the speed
      float PlayerSlow = 0.98;  //Creates a variable for slowing the player
      float PlayerDirection = 0;  //Creates a variable for the direction of the player
      float TurnSpeed = 3;  //Creates a variable for the speed the player can turn
      float ShipScale=1;  //Creates a variable for the size of the ship
    
      PShape SpaceshipShape;
    
      void setup() {
    
    PShape SpaceshipShape, ship, thruster1, thruster2, flame1, flame2;  //Creates the shape classes to create the Spaceship
    
    ship = createShape();  //Creates the shape ship
    ship.beginShape();  //Begins the shape
    ship.fill(230);  //Sets the colour
    ship.stroke(0);  //Sets the stroke colour
    ship.rotate(radians(90));  //Re-orientates the shape
    ship.vertex(25, 0);  //Sets a series of vertex to draw the main ship body
    ship.vertex(0, 50);
    ship.vertex(25, 35);
    ship.vertex(50, 50);
    ship.vertex(25, 0);
    ship.endShape();  //Ends the shape
    
    //thruster1, thruster2, flame1 and flame2 use the same process as above
    
    thruster1 = createShape();
    thruster1.beginShape();
    thruster1.fill(125);
    thruster1.stroke(0);
    thruster1.rotate(radians(90));
    thruster1.vertex(15, 29);
    thruster1.vertex(20, 29);
    thruster1.vertex(20, 50);
    thruster1.vertex(15, 50);
    thruster1.endShape();
    
    thruster2 = createShape();
    thruster2.beginShape();
    thruster2.fill(125);
    thruster2.stroke(0);
    thruster2.rotate(radians(90));
    thruster2.vertex(30, 29);
    thruster2.vertex(35, 29);
    thruster2.vertex(35, 50);
    thruster2.vertex(30, 50);
    thruster2.endShape();
    
    flame1 = createShape();
    flame1.beginShape();
    flame1.fill(255, 0, 0);
    flame1.stroke(0);
    flame1.rotate(radians(90));
    flame1.vertex(15, 50);
    flame1.vertex(20, 50);
    flame1.vertex(17.5, 58);
    flame1.endShape();
    
    flame2 = createShape();
    flame2.beginShape();
    flame2.fill(255, 0, 0);
    flame2.stroke(0);
    flame2.rotate(radians(90));
    flame2.vertex(30, 50);
    flame2.vertex(35, 50);
    flame2.vertex(32.5, 58);
    flame2.endShape();
    
    SpaceshipShape = new PShape();
    SpaceshipShape = createShape(GROUP);
    SpaceshipShape.addChild(ship);  //Adds the ship shape to the Spaceship group
    SpaceshipShape.addChild(thruster1);  //Adds the thruster1 shape to the Spaceship group
    SpaceshipShape.addChild(thruster2);  //Adds the thruster 2 shape to the Spaceship group
      }
    
    
      void forward() {
         PlayerSpeed += 0.1;   //The speed is incremented
        TurnSpeed += 0.1;   //The turn speed is incremented
      }
    
      void slow() {
        PlayerSlow=0.9;  //If S is pressed the slow speed is increased
      }
    
      void slow2() {
        PlayerSlow=0.98;  //Otherwise it remains at 0.98
      }
    
      void speedslow() {
        PlayerSpeed=PlayerSpeed*PlayerSlow;  //The player speed is slowed by        multiplying it with the slowing speed
      }
    
      void left() {
        PlayerDirection -=radians(TurnSpeed);   //The player rotates left according to the turn speed
      }
    
      void right() {
        PlayerDirection +=radians(TurnSpeed);   //the player rotates right according to the turn speed
      }
    
      void die() {
        PlayerDirection+=0.3;   //The player spins continously
        ShipScale-=0.01;  //The spaceship gets smaller
        if (ShipScale<=0) {  //If the spaceship scale is 0
          ShipScale=0;  //It remains set at 0
        }
      }
    
      void draw() {
    //PShape SpaceshipShape;
    
    translate(PosX, PosY);  //Translates the ship to the PosX and PosY variables
    rotate(PlayerDirection);  //Rotates the ship according to the direction variable
    scale(ShipScale);  //Scales the ship with the scaling variable
    shape(SpaceshipShape, 0, 0);  //Draws the shape Spaceship
    
    PosX+=cos(PlayerDirection)*PlayerSpeed;  //Calculates the X position acorrding to the players direction and speed
    PosY+=sin(PlayerDirection)*PlayerSpeed;  //Calculates the Y position acorrding to the players direction and speed
      }
    }
    
  • similar error to that I adressed in my last post :

    line 14 (!!!!!)

    PShape SpaceshipShape

    don't repeat SpaceshipShape here!!!!!

    because then you are working with that local version of SpaceshipShape which is only known inside the constructor and not in the entire class:

    • line 72 etc. is referring to line 14 (falsely) and not to line 10

    thus SpaceshipShape in line 10 remains null hence your npe in line 119

    it's a matter of the scope

  • I think you need to reformat your code. Your void setup() and void draw() are inside the spaceship class. If I run your sketch as you pasted it there I get nothing at all, save the default 100x100 grey rectangle.

    Move your setup and draw methods outside of the spaceship class and create a constructor to replace setup() and a method like update() to replace draw in this class.

  • well, maybe post your entire code

  • edited February 2016

    colouredmirrorball is right....

    I mean you can in theory call a method in a class draw() but only when you have the standard draw outside the class as well.... but instead of draw() convention is to use the word display() here as the name of the method in the class (it's confusing to use draw() as a name, because the standard draw() is so important)

    The "setup" part in the class is actually the constructor. It must have the same name as the class spaceship and never setup!!!!! (in theory you could have a method setup in the class but again only when you have a standard function setup outside the class and a constr in the class; and setup for a function that is not the standard setup would be a very confusing naming again)

    see tutorial :

    https://www.processing.org/tutorials/objects/

    Please post your entire sketch.

    here is a standard layout of a sketch with a class:

    DrawDot dd1 = new DrawDot(50, 80);
    
    void setup() { 
      size(200, 200);
      background(100);
    } 
    
    void draw() {
      background(100); 
      dd1.display();
    } 
    
    // =========================================
    
    class DrawDot {
    
    
      // the properties 
      int xpos, ypos;
    
    
      // the constructor copies the parameter it gets into the properties  
      DrawDot(int x, int y) {
        xpos = x;
        ypos = y;
      } // the constructor 
    
      // a method : 
      void display() {
        ellipse(xpos, ypos, 200, 200);
      } // method 
      //
    } // class
    //
    
  • and use ctrl-t in processing please to get auto format for indents

    you can see which { belongs to which }

    thank you!

  • Thank you all, I managed to solve the issue and the code works!

Sign In or Register to comment.