Home > Software engineering >  Null pointer exception when referencing an object
Null pointer exception when referencing an object

Time:12-14

I'm writing a dice roller program for a project, and it prints an error when I try to access the Calculations object. Everything else works, from what I can tell (the variables are set correctly, everything prints in order.

Calculations c;
int stage = 0;
int number;
int die;
int mod;
int modnum;
int inc = 0;
int[] num = new int[20];
boolean plusminus;
int spot = 0;
void setup() {
  startScreen(inc);
}

void draw() {
}
//initial prompt screen
void startScreen(int stage) {
  switch(stage) {
  case 0:
    println("How many dice? (up to 9)");
    break;
  case 1:
    number = num[spot];
    //noLoop();
    println("Modifiers? (press   or -)");
    break;
  case 2:
    mod = num[spot];
    if (mod == 10) {
      plusminus = true;
    } 
    if (mod == 11) {
      plusminus = false;
    }
    //noLoop();
    println("how much?");
    //loop();
    break;
  case 3:
    modnum = num[spot];
    //noLoop();
    println("which dice? (1 = d2, 2 = d3, 3 = d4, 4 = d6, 5 = d8, 6 = d10, 7 = d12, 8 = d20, 9= d100");
    break;
  case 4:
    die = num[spot];
    c.finalcalc();
    break;
  }
}

void keyPressed() {

  if (keyPressed) {
    if (keyCode == ENTER) {
      inc  ;
      loop();
      startScreen(inc);
      noLoop();
    }

    if (keyCode != ENTER) {
      if (key == '1') {
        num[spot] = 1;
        println(num[spot]);
      } else if (key == '2') {
        num[spot] = 2;
        println(num[spot]);
      } else if (key == '3') {
        num[spot] = 3;
        println(num[spot]);
      } else if (key == '4') {
        num[spot] = 4;
        println(num[spot]);
      } else if (key == '5') {
        num[spot] = 5;
        println(num[spot]);
      } else if (key == '6') {
        num[spot] = 6;
        println(num[spot]);
      } else if (key == '7') {
        num[spot] = 7;
        println(num[spot]);
      } else if (key == '8') {
        num[spot] = 8;
        println(num[spot]);
      } else if (key == '9') {
        num[spot] = 9;
        println(num[spot]);
      } else if (key == ' ') {
        num[spot] = 10;
        println(num[spot]);
      } else if (key == '-') {
        num[spot] = 11;
        println(num[spot]);
      }
    }
  }
}

class Calculations {
  int dice() {
    int i = 0;
    if (die == 1) {
      i = round(random(2));
    }
    if (die == 2) {
      i = round(random(3));
    }
    if (die == 3) {
      i = round(random(4));
    }
    if (die == 4) {
      i = round(random(6));
    }
    if (die == 5) {
      i = round(random(8));
    }
    if (die == 6) {
      i = round(random(10));
    }
    if (die == 7) {
      i = round(random(12));
    }
    if (die == 8) {
      i = round(random(20));
    }
    if (die == 9) {
      i = round(random(100));
    }
    return i;
  }
  int finalcalc() {
    int[] roll = new int[key];
    int result = 0;
    for (int i = 0; i > roll.length; i  ) {
      roll[i] = dice();
      if (plusminus == true) {
        result = dice()   modnum;
      }
      if (plusminus == false) {
        result = dice() - modnum;
      }
    } 
    return result;
  }
}

I've made sure that my arrays are initialized properly, and I made sure that each of my variables are being referenced correctly, but this hasn't changed anything. I'm not sure what else I need to do.

Here is the error:

java.lang.NullPointerException: Cannot invoke "Final_Project$Calculations.finalcalc()" because "this.c" is null
    at Final_Project.startScreen(Final_Project.java:68)
    at Final_Project.keyPressed(Final_Project.java:80)

CodePudding user response:

It's missing : Calculations c= new Calculations();

CodePudding user response:

Try adding constructors and destructors to your calculation class. It's almost always needed. This might also be the source of your nullptr.

  • Related