# Sieve of Eratosthenes Crash! NullPointerException

**URL:** <https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [July 7, 2020, 2:04pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450 "2020-07-07T14:04:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxnotabletoprogramm](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@Maxnotabletoprogramm](https://discourse.processing.org/u/Maxnotabletoprogramm)\
**Post date:** [July 7, 2020, 2:04pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/1 "2020-07-07T14:04:05Z")

</div>

Hey, Im currently trying(!) to make a sieve of eratosthenes in processing as a school project.  
This is my code:

int max=1500; //maximale zahl für die Primzahlen berechnet werden sollen  
int p=2; //  
boolean[] prim; //array in dem die Primzahlen stehen  
int[] fertige\_primzahlen;  
int f=0;

void setup() {

size(1000, 800);  
background(255);  
frameRate(20);  
headline();  
zeichnezahlen();  
prim= new boolean[max];  
fertige\_primzahlen= new int[100];  
init\_zahlen();  
}

void headline(){ //Zeichnet die Überschrift und den Text darunter  
textSize(26);  
fill(255, 140, 0);  
text(“Sieb des Eratosthenes”,2,20);  
textSize(10);  
fill(150);  
text(“Durch Drücken der N-Taste (next) kommt die nächste Vielfachreihe dran”,2,35);  
}

void zeichnezahlen() { //draws the numbers  
textSize(12);  
fill(0,0,0);  
for (int z=1; z\<max; z++) {

if(prim[z]){ //markiert die Primzahlen in Rot  
fill(255,0,0);  
}  
else {  
fill(0,255,0);  
}  
text(z,((z-(z-1)/28\*28)-1)_35,20_((z-1)/28)+50);  
}  
}

void init\_zahlen(){ //setzt alle zahlen standartmäßig auf prim  
for (int p=1;p\<max;p++){  
prim[p]=true;

}  
}

void next\_step(int v) {  
for (int n=2;n\<max/v;n++){  
prim[v\*n]=false;

}  
}

void get\_next\_v(){  
if (prim[p]) {  
next\_step§;  
zeichnezahlen();  
fertige\_primzahlen[f]=p;  
f++;  
p++;  
}  
else{  
p++;  
draw();  
}  
}

void draw(){  
if (keyPressed) {  
if (key == ‘n’ || key == ‘N’) {

fill(255);  
noStroke();  
quad(360,10,380,30,500,30,500,10);  
fill(0);  
text(p,400,20);  
get\_next\_v();  
}  
}  
}

Im German so dont mind the names but I think everything should be understandable.

So my Problem is: Everytime I start the program nothing happens and Windows tells me that it crashed. I really dont know what I’ve made wrong ☹

---

<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:** [July 7, 2020, 2:09pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/2 "2020-07-07T14:09:01Z")

</div>

Hello,

Please format your code as a courtesy to the community, for readability and to make it easy to copy code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code) \< Open this link

Try to _cut and paste_ your code into the Processing editor and you will understand.

When you hover over the code example below the copy icon pops up in upper right.

Example:

```auto
void setup() 
	{
  size(700, 150);
  background(0);
  fill(0, 255, 0);
  textAlign(CENTER, CENTER);
  textSize(48);
  text("Please format your code. :)", width/2, height/2-10);
	}

```

It’s that easy!

`:)`

---

<div class="post-metadata">

**Author:** ![Maxnotabletoprogramm](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@Maxnotabletoprogramm](https://discourse.processing.org/u/Maxnotabletoprogramm)\
**Post date:** [July 7, 2020, 2:34pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/3 "2020-07-07T14:34:12Z")

</div>

```auto
int max=1500; //maximale zahl für die Primzahlen berechnet werden sollen
int p=2; //
boolean[] prim; //array in dem die Primzahlen stehen
int[] fertige_primzahlen;
int f=0;

void setup() {
 
  size(1000, 800);
  background(255);
  frameRate(20);
  headline();
  zeichnezahlen();
  prim= new boolean[max];
  fertige_primzahlen= new int[100];
  init_zahlen();
 }

void headline(){ //Zeichnet die Überschrift und den Text darunter
  textSize(26);
  fill(255, 140, 0);
   text("Sieb des Eratosthenes",2,20); 
  textSize(10);
  fill(150);
   text("Durch Drücken der N-Taste (next) kommt die nächste Vielfachreihe dran",2,35); 
 }

void zeichnezahlen() { //Zeichnet die Zahlen und markiert alle primzahlen in rot.
 textSize(12);
 fill(0,0,0);
for (int z=1; z<max; z++) {
   
  if(prim[z]){ //markiert die Primzahlen in Rot
    fill(255,0,0);
    }
  else {
    fill(0,255,0);
    }
  text(z,((z-(z-1)/28*28)-1)*35,20*((z-1)/28)+50);
  }
 }
 

void init_zahlen(){ //setzt alle zahlen standartmäßig auf prim
  for (int p=1;p<max;p++){  
    prim[p]=true;
    
  }
}

void next_step(int v) {
  for (int n=2;n<max/v;n++){
    prim[v*n]=false;
  
  }
}

void get_next_v(){
  if (prim[p]) {
    next_step(p);
    zeichnezahlen();
    fertige_primzahlen[f]=p;
    f++;
    p++;
  }
  else{
    p++;
    draw();
  }
}

void draw(){
 if (keyPressed) {
 if (key == 'n' || key == 'N') {
   
   
   fill(255);
   noStroke();
   quad(360,10,380,30,500,30,500,10);
   fill(0);
   text(p,400,20);
   get_next_v();
  }
 }
}

```

---

<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:** [July 7, 2020, 3:46pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/4 "2020-07-07T15:46:34Z")

</div>

You must initialize array otherwise contents are _null:_

```auto
prim= new boolean[max];
  for (int i = 0; i<max; i++)
   {
   prim[i] = true;
   print("test" + prim[i]);
   }

```

[https://processing.org/reference/null.html](https://processing.org/reference/null.html)  
[https://processing.org/reference/Array.html](https://processing.org/reference/Array.html)  
[https://processing.org/tutorials/arrays/](https://processing.org/tutorials/arrays/)

`:)`

---

<div class="post-metadata">

**Author:** ![Maxnotabletoprogramm](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@Maxnotabletoprogramm](https://discourse.processing.org/u/Maxnotabletoprogramm)\
**Post date:** [July 7, 2020, 4:12pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/5 "2020-07-07T16:12:20Z")

</div>

So I have to set boolean prim to true? Im really bad sorry 🙂

---

<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:** [July 7, 2020, 4:24pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/6 "2020-07-07T16:24:51Z")

</div>

> [@Maxnotabletoprogramm](#):
>
> Im really bad sorry

Don’t be sorry… just start working on your project.

Simple questions and apologies suggest that you are not making an effort.  
Prove me wrong.

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate and use them.

This is a very short list:

> **Resources \< Click here to expand !**
>
> I encourage you to review the resources available here:
> 
> - The Processing _website_ has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the tabs will reveal what is available; libraries, tools and local examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)  
> Many of the Processing functions are here:  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - And the Internet. `:)`

There may be something of interest in there for you.

`:)`

---

<div class="post-metadata">

**Author:** ![Maxnotabletoprogramm](https://avatars.discourse-cdn.com/v4/letter/m/85e7bf/32.png) [@Maxnotabletoprogramm](https://discourse.processing.org/u/Maxnotabletoprogramm)\
**Post date:** [July 7, 2020, 5:25pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/7 "2020-07-07T17:25:47Z")

</div>

```auto
int max=1500; //maximale zahl für die Primzahlen berechnet werden sollen
boolean[] prim; //array in dem die Primzahlen stehen
int[] fertige_primzahlen;
int akt=2;

void setup() {
 
  size(1000, 800);
  background(255);
  frameRate(20);
  headline();
  zeichnezahlen();
  prim=new boolean[max];
  //next_step(2);
  init_zahlen();

 }

void headline(){ //Zeichnet die Überschrift und den Text darunter
  textSize(26);
  fill(255, 140, 0);
   text("Sieb des Eratosthenes",2,20); 
  textSize(10);
  fill(150);
   text("Durch Drücken der N-Taste (next) kommt die nächste Vielfachreihe dran",2,35); 
 }

void zeichnezahlen() { //schreibt alle Zahlen ins Fenster
 textSize(12);
 fill(0,0,0);
 for (int z=2; z<max; z++){
   
   if(prim[z]){
     fill(0,255,0);
   }
   else{
     fill(255,0,0);
   }

  text(z,((z-(z-1)/28*28)-1)*35,20*((z-1)/28)+50);
  }
}

void init_zahlen(){
  for (int i=1; i<max;i++){
    prim[i]=true;
  }
}

void next_step(int v){
  for (int m=2;m<max/v;m++){
    prim[m*v]=false;
  }
}

void draw() {
  if (keyPressed) {
    if (key == 'n' || key == 'N') {
      akt++;
      next_step(akt);
    }
  }
}

```

Hey, so now I made this. I get the error NullPointerException and Target VM failed to initialize. I thought that for the NullPointerException I just need to “create” the prim boolean in void setup and set z in zeichnezahlen (means drawnumbers) to 1. But thats not the case. Im pretty sure it should work if I can fix those issues…

---

<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:** [July 7, 2020, 8:49pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/8 "2020-07-07T20:49:55Z")

</div>

Hello,

You need to initialize the array _before you use it_.

Review the setup() and consider the order of the code that you are executing from the top down.

`:)`

---

<div class="post-metadata">

**Author:** ![BenDover](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@BenDover](https://discourse.processing.org/u/BenDover)\
**Post date:** [June 20, 2022, 4:29pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/9 "2022-06-20T16:29:02Z")

</div>

Hello i wanted to ask how it should be initialize

---

<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:** [June 20, 2022, 5:26pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/10 "2022-06-20T17:26:41Z")

</div>

Hello,

This has been answered here:

> [@Sieve of Eratosthenes Crash! NullPointerException](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/8):
>
> Hello, You need to initialize the array before you use it. Review the setup() and consider the order of the code that you are executing from the top down. slight_smile

Scrutinize the code and modify the order in `setup()`.

`:)`

---

<div class="post-metadata">

**Author:** ![BenDover](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@BenDover](https://discourse.processing.org/u/BenDover)\
**Post date:** [June 21, 2022, 3:35pm UTC](https://discourse.processing.org/t/sieve-of-eratosthenes-crash-nullpointerexception/22450/11 "2022-06-21T15:35:07Z")

</div>

thank you didn’t see that
