I can't see the issue in my code. (Stanley Cup, Null Pointer Exception)

edited November 2016 in Questions about Code

Hello everyone! I am pretty new to Processing and I am working on a final project due in a couple weeks where I am trying to create a game where the Blackhawks logo chases the Stanley Cup. But I can't keep adjusting it because it won't run due to a NullPointerException on line 62. Can any of you guys see something that I'm not seeing? Any help is crazy appreciated!

    final int stateGame  = 0;
    final int stateHome = 1;
    int state = stateHome;
    int angle = 0;
    int time = 0;
    int[] headx= new int[2500];
    int[] heady= new int[2500];
    int fruitx=(round(random(10))+1)*8;
    int fruity=(round(random(10))+1)*8;
    int skatex = 0;
    int skatey = 0;
    int snakesize=5;
    boolean startover = true;
    boolean stopgame=false;
    PFont Silom;
    PImage Rink;
    PImage BH;
    PImage SC;

    void setup()
    {
      size(684, 361);
      Rink = loadImage("Rink.jpg");
      BH = loadImage("BH.png");
      SC = loadImage("SC.png");
      Silom = loadFont("Silom.vlw");
      background(Rink);
      restart();
    } 

    void draw() 
    { 

      switch (state) {
      case stateGame: 
        handleStateGame();
        break; 

      case stateHome:
        handleStateHome(); 
        break;

      default:
        println("Game has begun"
          + state 
          + ".");
        exit();
        break;
      }
    }

    void handleStateGame() {
      background(Rink);
    }
    {    
      if(stopgame)
      {
      } else
      {
       time+=1;

       image(SC,fruitx, fruity, 15, 15);
      }
      if ((time % 4)==0)
      {
        travel();
        display();
        checkdead();
      }}

      void travel()
      {
        for (int i=snakesize; i>0; i--)
        {
          if (i!=1)
          {
            headx[i]=headx[i-1];
            heady[i]=heady[i-1];
          } else
          {
            switch(angle)
            {
            case 0:
              headx[1]+=8;
              break;
            case 90:
              heady[1]-=8;
              break;
            case 180:
              headx[1]-=8;
              break;
            case 270:
              heady[1]+=8;
              break;
            }
          }
        }
      }
      void display()
      {
        //is the head eating the apple?
        if (headx[1]==fruitx && heady[1]==fruity)
        {
          startover=true;
          while (startover)
          {
            snakesize+=round(random(3)+1);
            //grow and spawn the apple somewhere away
            fruitx=(round(random(5))+1);
            fruity=(round(random(47))+1)*8;
            for (int i=1; i<snakesize; i++)
            {
              if (fruitx==headx[i] && fruity==heady[i])
              {
                startover=true;
              } else
              {
                startover=false;
                i=1000;
              }
            }
          }
        }
        image(BH, headx[1], heady[1], 8, 8);
      }
      void checkdead()
      {
        for (int i=0; i<=snakesize; i++)
        {
          if (headx[1]>=(width-8) || heady[1]>=(height-8) || headx[1]<=0 || heady[1]<=0)
          {
            fill(0);
            text("RESTART", 300, 350);
            text("Press Spacebar.", 300, 300);
            stopgame=true;
          }
        }
      }
      void restart()
      {
        background(255);
        headx[1]=400;
        heady[1]=400;
        for (int i=2; i<1000; i++)
        {
          headx[i]=0;
          heady[i]=0;
        }
        stopgame=false;
        fruitx=(round(random(10))+2)*8;
        fruity=(round(random(10))+2)*8;
        time=0;
        angle=0;
        startover = true;
      }

      void handleStateHome() {
        background(Rink);
        fill(0);
        textFont(Silom, 30);
        text("Press Space to Begin", 185, 260);
        textFont(Silom, 20);
        text("Press the arrow keys to move around", 150, 220);
        textFont(Silom, 60);
        text("Catch the cup!", 122, 190);
      }

      void keyPressed() {

        switch (state) {

        case stateGame: 
          keyPressedForStateGame();
          break; 

        case stateHome:
          keyPressedForStateHome(); 
          break;

        default:
          println("Game has begun"
            + state 
            + ".");
          exit();
          break;
        }
      }

      void keyPressedForStateGame() { 
        if (key == CODED) 
        {
          if(keyCode == UP) {  if(heady[1] != heady[0]-10){skatey = -10; skatex = 0;}}
      if(keyCode == DOWN) {  if(heady[1] != heady[0]+10){skatey = 10; skatex = 0;}}
      if(keyCode == LEFT) { if(headx[1] != headx[0]-10){skatex = -10; skatey = 0;}}
      if(keyCode == RIGHT) { if(headx[1] != headx[0]+10){skatex = 10; skatey = 0;}}
      if(keyCode == ' ')
            state = stateHome;
         restart();
        }
      }

      void keyPressedForStateHome() { 
        if (key == ' ') {
          state = stateGame;
        }
      }
Tagged:

Answers

  • SC is probably null. Does SC.png exist? Do you get a red "could not load" warning? If it fails try replacing the image() statement by an ellipse to continue development until you can fix the issue

  • edited November 2016

    or just add println("SC:" + SC); on line 61 to see if it's null

  • but it's not that...

    note this bit of code, starting line 52...

        void handleStateGame() {
          background(Rink);
        }
        {    
          if(stopgame)
          {
          } else
          {
           time+=1;
    
           image(SC,fruitx, fruity, 15, 15);
          }
    

    the handleStateGame() method finishes on line 54

    on line 55 a static block starts, this runs before setup(), and SC isn't initialised then.

    i guess lines 54 and 55 are erroneous and that if (stopgame)... is part of handleStateGame

  • and this is why we use ctrl-t to format our code nicely...

Sign In or Register to comment.