Why does my code give NullPointerException error?

Namaste all,

Why does the floowing code give a null point exception error? Processing highlights the line where I have called the line function. This is a p5js code which I am trying to recreate in processing.

Thanks.

void setup() {
  size(500, 500);
  colorMode(HSB, 360, 100, 100, 100);
  PGraphics tex = createGraphics(width, height);
  tex.colorMode(HSB, 360, 100, 100, 100);
  
  for (int i = 0; i < (width * height) / 100; i++) {
    if (random(360) < 0.5) {
      tex.stroke(0, 0, 100, 2);
    } else {
      tex.stroke(0, 0, 0, 1);
    }
    float cx1 = random(width);
    float cy1 = random(height);
    float angle = random(360);
  
    tex.line(cx1 + sin(radians(angle)) * width, cy1 + cos(radians(angle)) * height, cx1 + sin(radians(angle + 180)) * width, cy1 + cos(radians(angle + 180)) * height);
  }
}

void draw(){
}

Try using beginDraw() and endDraw(). Reference is here: https://processing.org/reference/PGraphics.html

Will also need a global variable: PGraphics tex;

Call the PGraphics in draw() with image(tex, 0, 0);

[Code removed]

2 Likes

Thanks for the help :smiley:

The code included in this post has been taken out because it completely solved the problem,

Only actual things @svan did was globally declaring variable tex w/ PGraphics tex;, plus adding tex.beginDraw(); & tex.endDraw(); to the OP’s already provided code!

, instead of just providing pre-made answers.

There’s nothing to provide but link to very basic requirements for dealing w/ PGraphics objects.

1 Like

Thanks for the help

You’re quite welcome; we are always happy to help with coding issues. Good luck with your project.

Hello @prathmesh,

There are lots of resources (tutorials, references, examples, etc.) here:

Related to your topic:

Be careful to scrutinize code carefully and learn from it.

The code that was removed had the tex.endDraw() inside the loop which may not yield the intended or desired results.
You can try it inside the loop and outside of the loop to see the difference.

Have fun!

:)

1 Like

How do you know that?? It’s been removed. I seriously doubt that I would have posted code which did not run.

We can view the history of edits via the pencil :pencil2: icon. :wink:

The initial post was not edited to the best of my knowledge. It was only edited after someone deleted the code and thereby changed the answer.

Excerpt of the original post before any moderation edits:

    tex.line(cx1 + sin(radians(angle)) * width, cy1 + cos(radians(angle)) * height, cx1 + sin(radians(angle + 180)) * width, cy1 + cos(radians(angle + 180)) * height);
    tex.endDraw();
  }
}

The code does run either way, but the output is markedly different. Apparently I did not keep a copy and just deleted it after I solved the null pointer error, so it is possible it was initially inside the for loop when obviously it should be outside. I always assumed I could always go back to the original post and see my code, but obviously that is not the case. Good to know that some of us have more editorial tools than others, and some have the power to just wipe away an answer.

1 Like