Can anyone tell me why I would be getting a null pointer exception?

edited April 2016 in Arduino

I am trying to call the healthBar class in one of my other classes. but for some reason it is giving null pointer.

The button variable is to determine whether a button is pressd

import processing.serial.*;
import cc.arduino.*;

public class House {
  int xSpeed;
  int button;
  PImage background;
  PImage dogSitting;
  float dogSittingx, dogSittingy;
  PImage dogLeft1;
  float dogLeft1x, dogLeft1y;
  PImage dogRight1;
  float dogRightx, dogRighty;
  boolean isMoving = false;
  HealthBar hb0;

  public void houseSetup() {
    //size(1800,900);
    String comPort = "COM3";

    background = loadImage("gameBackground.jpg");
    dogSitting = loadImage("startDog.png");
    dogLeft1 = loadImage("dogWalking1L.png");
    dogRight1 = loadImage("dogWalking1R.png");
    background.resize(1800, 900);

    button = port.read();
    println(button);
    if (button ==2) {
      println(button);
    } else if (button ==1) {
    }

    //dogSittingx = 800; dogSittingy=500;
    hb0.healthSetup(); 
    dogLeft1x =600; 
    dogLeft1y =300;
    xSpeed =3;
  }

  public void houseDraw() {

    button = port.read();

    if (button ==2) {
      isMoving = false;
      println(button);
    } else if (button == 1) {

      //hb0.healthDraw();

      println(button);
      isMoving = true;
      if (isMoving) {

        hb0.value = random( hb0.max );
        image(dogLeft1, dogLeft1x, dogLeft1y);

        if (dogLeft1x <=0) {
          xSpeed = -xSpeed; 
          imageFlip(dogLeft1, 1500, 400, "h");
        }

        if (dogLeft1x >=1500) {
          xSpeed = -xSpeed; 
          imageFlip(dogLeft1, 1500, 400, "h");
        }
        dogLeft1x =dogLeft1x - xSpeed;
      } else if (button == 3) {
        image(dogRight1, dogRightx, dogRighty); 
        println(button);

        if (dogRightx <=0) {
          xSpeed = -xSpeed; 
          imageFlip(dogRight1, 1500, 400, "h");
        }

        if (dogRightx >=1500) {
          xSpeed = -xSpeed; 
          imageFlip(dogRight1, 1500, 400, "h");
        }
        dogRightx =dogRightx - xSpeed;
      }

      background(background);
      //image(dogSitting,dogSittingx,dogSittingy);
      image(dogLeft1, dogLeft1x, dogLeft1y);
      hb0.healthDraw();
    }
  }

  void imageFlip(PImage imageName, int xPos, int yPos, String mode) {

    //"v" flips the entire image across the x-axis
    if (mode == "v") {

      imageName.loadPixels();
      int tempImage[] = new int[imageName.width*imageName.height];
      for (int i = 0; i < imageName.width*imageName.height; i++) {
        tempImage[i] = imageName.pixels[i];
      }

      for (int i = 0; i < imageName.height; i++) {
        for (int j = 1; j < imageName.width; j++) {
          imageName.pixels[(imageName.height - 1 - i)*(imageName.width) + j] = tempImage[i*(imageName.width) + j];
        }
      }
      imageName.updatePixels();
      image(imageName, xPos, yPos);

      //"h" flips the entire image across the y-axis
    } else if (mode == "h") {
      imageName.loadPixels();
      int tempImage[] = new int[imageName.width*imageName.height];
      for (int i = 0; i < imageName.width*imageName.height; i++) {
        tempImage[i] = imageName.pixels[i];
      }
      for (int i = 0; i < imageName.height; i++) {
        for (int j = 1; j < imageName.width; j++) {
          imageName.pixels[(i+1)*(imageName.width) - j] = tempImage[i*imageName.width + j];
        }
      }
      imageName.updatePixels();
      image(imageName, xPos, yPos);
    } else {
      println("No mirror direction specified!");
      println("Use v, v2, h, or h2 for the 4th argument");
    }
  }
} 

class HealthBar {
  float value, max, x, y, w, h;
  color backing, bar;

  HealthBar() {
    value = 50;
    max = 100;
    x = 10;
    y = 10;
    w = 200;
    h = 10;
    backing = color(255, 0, 0);
    bar = color(0, 255, 0);
  }

  void draw() {
    fill(backing);
    stroke(0);
    rect(x, y, w, h);
    fill(bar);
    rect(x, y, map(value, 0, max, 0, w), h);
  }

  HealthBar hb0;

  public void healthSetup() {
    size(220, 220);
    hb0 = new HealthBar();
  }

  public void healthDraw() {
    background(128);
    hb0.draw();
  }
}
Tagged:

Answers

  • (ctrl-t in the processing editor indents the code nicely

    paste the code, highlight the code and pressing ctrl-o to format the code for the forum.

    i've done it for you this time.)

  • Oh right sorry I forgot to format it in the processing editor

  • Answer ✓

    line 15 you define hb0 but don't initialise it.

    line 35 tries to use an uninitialised hb0. = NullPointerException.

  • lines 155 to 165 look unnecessary.

  • Great thanks I didnt spot that :)

  • edited April 2016

    Remember that programming in Processing revolves around settings() + setup() as the initializers for resources & draw() as the infinite loop which uses those resources.

    It seems to me your classes are usurping those roles. Thus you're unnecessarily rowing against Processing's way of doing things. 8-|

  • I am still only new to processing and am learning :/ programming isnt my strong suit :/

  • edited April 2016

    It's not about programming per se but the way Processing framework expects things to be done.
    Blocks of code such as this 1:

    background = loadImage("gameBackground.jpg");
    dogSitting = loadImage("startDog.png");
    dogLeft1 = loadImage("dogWalking1L.png");
    dogRight1 = loadImage("dogWalking1R.png");
    background.resize(1800, 900);
    

    are supposed to belong to setup(), not some other subordinate class.

    All resource loading is supposed to happen in setup() once.
    The problem in transferring them to other classes is that every time they're instantiated those resource loading unnecessarily happen again.
    And even worse, we end up having extra objects w/ the same content wasting RAM!

    Indeed encapsulation is very important. But even good rules need to bend in order to adapt to the environment. In this case Processing. >-)

    Remember that the sketch is itself a top class. All other classes are nested, subordinate to the sketch.

  • So are u saying all of my images should be loaded in the one setup() rather than having many setups in different classes?

  • Yes. That's what setup() is for, to "till" the environment for the draw().
    Once all resources are loaded, we can pass their references to the classes which need them via their constructors.

Sign In or Register to comment.