Sieve of Eratosthenes Crash! NullPointerException

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 :frowning:

1 Like

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 < 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:

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!

:)

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

You must initialize array otherwise contents are null:

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/Array.html
https://processing.org/tutorials/arrays/

:)

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

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:

There may be something of interest in there for you.

:)

1 Like
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…

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.

:)

Hello i wanted to ask how it should be initialize

Hello,

This has been answered here:

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

:)

thank you didn’t see that

1 Like