# Why does my program crash sometimes?

**URL:** https://discourse.processing.org/t/why-does-my-program-crash-sometimes/35497
**Category:** Coding Questions
**Created:** [March 1, 2022, 9:55am UTC](https://discourse.processing.org/t/why-does-my-program-crash-sometimes/35497 "2022-03-01T09:55:32Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Coder1](https://avatars.discourse-cdn.com/v4/letter/c/d9b06d/32.png) [@Coder1](https://discourse.processing.org/u/Coder1)
#### Post date: [March 1, 2022, 9:55am UTC](https://discourse.processing.org/t/why-does-my-program-crash-sometimes/35497/1 "2022-03-01T09:55:32Z")

</div>

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;  
}  
}  
}

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 1, 2022, 11:45am UTC](https://discourse.processing.org/t/why-does-my-program-crash-sometimes/35497/2 "2022-03-01T11:45:48Z")

</div>

More information needed

Do you get an error message?

When does it crash exactly? What happens before?

---

<div class="post-metadata">

### Author: ![Coder1](https://avatars.discourse-cdn.com/v4/letter/c/d9b06d/32.png) [@Coder1](https://discourse.processing.org/u/Coder1)
#### Post date: [March 1, 2022, 12:07pm UTC](https://discourse.processing.org/t/why-does-my-program-crash-sometimes/35497/3 "2022-03-01T12:07:41Z")

</div>

-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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [March 1, 2022, 1:04pm UTC](https://discourse.processing.org/t/why-does-my-program-crash-sometimes/35497/4 "2022-03-01T13:04:55Z")

</div>

Hello,

Related topic:

> [@Why am I having the NullPointerException error?](https://discourse.processing.org/t/why-am-i-having-the-nullpointerexception-error/31876/3):
>
> Hello, Try this: String FileName = ""; [image] I created a test.iL7 to test. Reference: [https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception](https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception)slight_smile

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

I am seeing this error with your code:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b229f208bd6bf99927098d251ddf228358c159f8.png)

Example:

```auto
// 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](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 1, 2022, 1:04pm UTC](https://discourse.processing.org/t/why-does-my-program-crash-sometimes/35497/5 "2022-03-01T13:04:58Z")

</div>

This error message is very important here

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 1, 2022, 1:10pm UTC](https://discourse.processing.org/t/why-does-my-program-crash-sometimes/35497/6 "2022-03-01T13:10:46Z")

</div>

> [@Coder1](#):
>
> clanRoll

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);

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [March 1, 2022, 3:51pm UTC](https://discourse.processing.org/t/why-does-my-program-crash-sometimes/35497/7 "2022-03-01T15:51:23Z")

</div>

hi

try this

```auto
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;
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 1, 2022, 4:44pm UTC](https://discourse.processing.org/t/why-does-my-program-crash-sometimes/35497/8 "2022-03-01T16:44:58Z")

</div>

Thanks for that.

Here is my version:

```auto

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;
    }
  }
}
//

```
