Why does my program crash sometimes?

Hello,

I made a Clan system where you get different advantages based on your clan. However my code does work but sometimes it just crashes and I don’t know why. Please help me

int clanRoll;
String clanName;
int clanColorRed;
int clanColorGreen;
int clanColorBlue;
int blackScreenR = 100;
int clanSpinned = 0;
int timerClanReroll = 0;
int timerLoadGame = 0;
boolean Menu = true;
boolean ClanMenu = false;
boolean BlackScreen = false;
boolean ableToSpin = true;
boolean loadGame = false;
boolean clanBackground = false;
void setup() {
size(displayWidth,displayHeight);
orientation(LANDSCAPE);
}
void draw() {
background(blackScreenR);
if (Menu == true) {
ellipse(width/2,height/2,200,200);
if (BlackScreen == true) {
blackScreenR -= 5;
}
if (blackScreenR <= 0) {
Menu = false;
//ClanMenu = false;
blackScreenR = 0;
}
}
if (blackScreenR < 100 && Menu == false && clanBackground == false && ableToSpin == true) {
blackScreenR += 5;
}
if (Menu == false && blackScreenR == 100 && clanBackground == false) {
//background(blackScreenR);
fill(255);
textSize(50);
text(“Clan:”,width/2.5,height/2);
clanSpinned = 1;
}
if (clanBackground == true) {
//background(blackScreenR);
}
if (Menu == false && ClanMenu == false && clanSpinned == 1 && blackScreenR >= 100) {
frameRate(5);
//background(100);
if (timerClanReroll < 10) {
clanRoll = (int)random(1,10000);
}
if (timerClanReroll == 10) {
clanRoll = (int)random(1,10000);
}
if (timerClanReroll > 10) {
loadGame = true;
clanBackground = true;
}
if (clanRoll == 1) {
clanName = “Kamado”;
clanColorRed = 255;
clanColorGreen= 0;
clanColorBlue = 0;
}
else if (clanRoll <= 100 && clanRoll > 1) {
clanName = “Himejima”;
clanColorRed = 0;
clanColorGreen= 0;
clanColorBlue = 0;
}
else if (clanRoll <= 1000 && clanRoll > 100) {
clanName = “Kanroji”;
clanColorRed = 255;
clanColorGreen= 0;
clanColorBlue = 193;
}
else if (clanRoll <= 2000 && clanRoll > 1000) {
clanName = “Shinazugawa”;
clanColorRed = 0;
clanColorGreen= 255;
clanColorBlue = 0;
}
else if (clanRoll <= 3000 && clanRoll > 2000) {
clanName = “Tomioka”;
clanColorRed = 0;
clanColorGreen= 0;
clanColorBlue = 255;
}
else if (clanRoll <= 4000 && clanRoll > 3000) {
clanName = “Kocho”;
clanColorRed = 168;
clanColorGreen= 12;
clanColorBlue = 232;
}
else if (clanRoll <= 5000 && clanRoll > 4000) {
clanName = “Agatsuma”;
clanColorRed = 255;
clanColorGreen= 255;
clanColorBlue = 0;
}
else if (clanRoll <= 6000 && clanRoll > 5000) {
clanName = “Tokito”;
clanColorRed = 21;
clanColorGreen= 230;
clanColorBlue = 223;
}
else if (clanRoll <= 8000 && clanRoll > 7000) {
clanName = “Uzui”;
clanColorRed = 255;
clanColorGreen= 255;
clanColorBlue = 255;
}
else if (clanRoll <= 10000 && clanRoll > 9000) {
clanName = “Hashibira”;
clanColorRed = 43;
clanColorGreen= 181;
clanColorBlue = 194;
}
fill(clanColorRed,clanColorGreen,clanColorBlue);
text(clanName,width/2,height/2);
timerClanReroll++;
}
if (loadGame == true) {
frameRate(60);
timerLoadGame += 100;
if (timerLoadGame >= 100 && clanBackground == true) {
blackScreenR -= 5;
}
if (blackScreenR == 0 && clanBackground == true) {
ableToSpin = false;
clanBackground = false;
clanSpinned = 0;
}
}
}
void mousePressed() {
if (Menu == true) {
if (dist(mouseX,mouseY,width/2,height/2) < 100) {
BlackScreen = true;
}
}
}

1 Like

More information needed

Do you get an error message?

When does it crash exactly? What happens before?

-So when you start the programm there is a white circle which you have to click on. Then you get a “Clan” assigned randomly. Then the background turns black because I wanted it to connect to my game but this is not really important.

-The programm sometimes crashes when you are about to get a “Clan”. But the programm doesn’t always crash. Sometimes it works perfectly but sometimes it crashes.

-This is what I get as an error:
java.lang.NullPointerException: Attempt to invoke virtual method ‘int java.lang.String.length()’ on a null object reference

Hello,

Related topic:

Where did you get the error? These may be highlighted when found as in example below.

I am seeing this error with your code:

Example:

// String s; // s is not initialized and is a null
String s = ""; // s is initialized and not a null

void setup()
  {
  text(s, 0, 0);
  }

Please format your code as a courtesy to our community:
https://discourse.processing.org/faq#format-your-code

:)

This error message is very important here

You don’t evaluate this completely

I think you miss 6000 to 7000 and 9000 to 10000

Check this

then clanName is undefined I guess

An else block at the end of the if…else if… block might help where you can say println (clanRoll);

1 Like

hi

try this

int clanRoll;
String d=  "clanName";
int clanColorRed;
int clanColorGreen;
int clanColorBlue;
int blackScreenR = 100;
int clanSpinned = 0;
int timerClanReroll = 0;
int timerLoadGame = 0;
boolean Menu = true;
boolean ClanMenu = false;
boolean BlackScreen = false;
boolean ableToSpin = true;
boolean loadGame = false;
boolean clanBackground = false;
void setup() {
  size(displayWidth, displayHeight);
  orientation(LANDSCAPE);
}
void draw() {
  background(blackScreenR);
  if (Menu == true) {
    ellipse(width/2, height/2, 200, 200);
    if (BlackScreen == true) {
      blackScreenR -= 5;
    }
    if (blackScreenR <= 0) {
      Menu = false;
      //ClanMenu = false;
      blackScreenR = 0;
    }
  }
  if (blackScreenR < 100 && Menu == false && clanBackground == false && ableToSpin == true) {
    blackScreenR += 5;
  }
  if (Menu == false && blackScreenR == 100 && clanBackground == false) {
    //background(blackScreenR);
    fill(255);
    textSize(50);
    text("Clan:", width/2.5, height/2);
    clanSpinned = 1;
  }
  if (clanBackground == true) {
    //background(blackScreenR);
  }
  if (Menu == false && ClanMenu == false && clanSpinned == 1 && blackScreenR >= 100) {
    frameRate(5);
    //background(100);
    if (timerClanReroll < 10) {
      clanRoll = (int)random(1, 10000);
    }
    if (timerClanReroll == 10) {
      clanRoll = (int)random(1, 10000);
    }
    if (timerClanReroll > 10) {
      loadGame = true;
      clanBackground = true;
    }
    if (clanRoll == 1) {
   d = "Kamado";
      clanColorRed = 255;
      clanColorGreen= 0;
      clanColorBlue = 0;
    } else if (clanRoll <= 100 && clanRoll > 1) {
     d = "Himejima";
      clanColorRed = 0;
      clanColorGreen= 0;
      clanColorBlue = 0;
    } else if (clanRoll <= 1000 && clanRoll > 100) {
   d = "Kanroji";
      clanColorRed = 255;
      clanColorGreen= 0;
      clanColorBlue = 193;
    } else if (clanRoll <= 2000 && clanRoll > 1000) {
   d = "Shinazugawa";
      clanColorRed = 0;
      clanColorGreen= 255;
      clanColorBlue = 0;
    } else if (clanRoll <= 3000 && clanRoll > 2000) {
      d= "Tomioka";
      clanColorRed = 0;
      clanColorGreen= 0;
      clanColorBlue = 255;
    } else if (clanRoll <= 4000 && clanRoll > 3000) {
    d= "Kocho";
      clanColorRed = 168;
      clanColorGreen= 12;
      clanColorBlue = 232;
    } else if (clanRoll <= 5000 && clanRoll > 4000) {
   d = "Agatsuma";
      clanColorRed = 255;
      clanColorGreen= 255;
      clanColorBlue = 0;
    } else if (clanRoll <= 6000 && clanRoll > 5000) {
     d = "Tokito";
      clanColorRed = 21;
      clanColorGreen= 230;
      clanColorBlue = 223;
    } else if (clanRoll <= 8000 && clanRoll > 7000) {
     d= "Uzui";
      clanColorRed = 255;
      clanColorGreen= 255;
      clanColorBlue = 255;
    } else if (clanRoll <= 10000 && clanRoll > 9000) {
     d = "Hashibira";
      clanColorRed = 43;
      clanColorGreen= 181;
      clanColorBlue = 194;
    }
    fill(clanColorRed, clanColorGreen, clanColorBlue);
    text(d, width/2, height/2);
    timerClanReroll++;
  }
  if (loadGame == true) {
    frameRate(60);
    timerLoadGame += 100;
    if (timerLoadGame >= 100 && clanBackground == true) {
      blackScreenR -= 5;
    }
    if (blackScreenR == 0 && clanBackground == true) {
      ableToSpin = false;
      clanBackground = false;
      clanSpinned = 0;
    }
  }
}
void mousePressed() {
  if (Menu == true) {
    if (dist(mouseX, mouseY, width/2, height/2) < 100) {
      BlackScreen = true;
    }
  }
}

Thanks for that.

Here is my version:



int clanRoll;
String clanName="";
int clanColorRed;
int clanColorGreen;
int clanColorBlue;
int blackScreenR = 100;
int clanSpinned = 0;
int timerClanReroll = 0;
int timerLoadGame = 0;
boolean Menu = true;
boolean ClanMenu = false;
boolean BlackScreen = false;
boolean ableToSpin = true;
boolean loadGame = false;
boolean clanBackground = false;

// ------------------------------------------------------------------------------------
// setup() and draw()

void setup() {
  size(displayWidth, displayHeight);
  orientation(LANDSCAPE);
}

void draw() {
  background(blackScreenR);

  if (Menu) {
    drawForMenu();
  } else {
    drawForMenuIsOver();
  }
}//draw()

//---------------------------------------------------------------------------------------
//called directly by draw()

void drawForMenu() {
  fill(255); 
  ellipse(width/2, height/2, 200, 200);
  if (BlackScreen == true) {
    blackScreenR -= 5;
  }
  if (blackScreenR <= 0) {
    Menu = false;
    //ClanMenu = false;
    blackScreenR = 0;
  }
}

void drawForMenuIsOver() {

  if (blackScreenR < 100 && clanBackground == false && ableToSpin == true) {
    blackScreenR += 5;
  }

  if (blackScreenR == 100 && clanBackground == false) {
    //background(blackScreenR);
    fill(255);
    textSize(50);
    text("Clan:", width/2.5, height/2);
    clanSpinned = 1;
  }

  // if (clanBackground == true) {
  //background(blackScreenR);
  // }

  if (ClanMenu == false && clanSpinned == 1 && blackScreenR >= 100) {
    frameRate(5);

    if (timerClanReroll < 10) {
      clanRoll = (int)random(1, 10000);
    }
    if (timerClanReroll == 10) {
      clanRoll = (int)random(1, 10000);
    }
    if (timerClanReroll > 10) {
      loadGame = true;
      clanBackground = true;
    }

    // -----
    //
    evalClanRoll();

    fill(clanColorRed, clanColorGreen, clanColorBlue);
    text(clanName, width/2, height/2);
    timerClanReroll++;
  }

  // ---

  if (loadGame) {
    frameRate(60);
    timerLoadGame += 100;
    if (timerLoadGame >= 100 && clanBackground) {
      blackScreenR -= 5;
    }
    if (blackScreenR == 0 && clanBackground) {
      ableToSpin = false;
      clanBackground = false;
      clanSpinned = 0;
    }
  }
}

// -----------------------------------------------------------------------------------
// secondary functions to that 

void  evalClanRoll() {
  // 

  if (clanRoll == 1) {
    clanName = "Kamado";
    clanColorRed = 255;
    clanColorGreen= 0;
    clanColorBlue = 0;
  } else if (clanRoll <= 100 && clanRoll > 1) {
    clanName = "Himejima";
    clanColorRed = 111;
    clanColorGreen= 111;
    clanColorBlue = 111;
  } else if (clanRoll <= 1000 && clanRoll > 100) {
    clanName = "Kanroji";
    clanColorRed = 255;
    clanColorGreen= 0;
    clanColorBlue = 193;
  } else if (clanRoll <= 2000 && clanRoll > 1000) {
    clanName = "Shinazugawa";
    clanColorRed = 0;
    clanColorGreen= 255;
    clanColorBlue = 0;
  } else if (clanRoll <= 3000 && clanRoll > 2000) {
    clanName = "Tomioka";
    clanColorRed = 0;
    clanColorGreen= 0;
    clanColorBlue = 255;
  } else if (clanRoll <= 4000 && clanRoll > 3000) {
    clanName = "Kocho";
    clanColorRed = 168;
    clanColorGreen= 12;
    clanColorBlue = 232;
  } else if (clanRoll <= 5000 && clanRoll > 4000) {
    clanName = "Agatsuma";
    clanColorRed = 255;
    clanColorGreen= 255;
    clanColorBlue = 0;
  } else if (clanRoll <= 6000 && clanRoll > 5000) {
    clanName = "Tokito";
    clanColorRed = 21;
    clanColorGreen= 230;
    clanColorBlue = 223;
  } else if (clanRoll <= 8000 && clanRoll > 7000) {
    clanName = "Uzui";
    clanColorRed = 255;
    clanColorGreen= 255;
    clanColorBlue = 255;
  } else if (clanRoll <= 10000 && clanRoll > 9000) {
    clanName = "Hashibira";
    clanColorRed = 43;
    clanColorGreen= 181;
    clanColorBlue = 194;
  } else {
    println("Error: reached else: with " +clanRoll);
  }// else
}//

void mousePressed() {
  if (Menu) {
    if (dist(mouseX, mouseY, width/2, height/2) < 100) {
      BlackScreen = true;
    }
  }
}
//