set() method throws NullPointerException for PGraphicsOpenGL objects #1520
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Most appropriate sub-area of Processing 4?
Core/Environment/Rendering
Processing version
4.5.2
Operating system
Linux (Unbuntu) and MacOS Tahoe
Bug description
I get a null pointer exception when I pass a PGraphics object to the background() method.
For some reason the PDE doesn't display the stack trace, I just see this:
NullPointerException
NullPointerExceptionSteps to reproduce this
The example code below reproduces the bug.
snippet
PGraphics g;
void setup() {
size(300, 300, P3D);
g = createGraphics(width, height, P2D);
g.beginDraw();
g.background(0, 255, 0);
g.endDraw();
}
void draw() {
background(g);
}I am able to get a stack trace when I use the equivalent code in py5. I traced the problem to the PGraphics object's null pixels array.
One possible workaround is to insert a g.loadPixels() call after g.endDraw(). That fixes the problem. Another workaround is to get() a PImage object:
PGraphics g;
PImage img;
void setup() {
size(300, 300, P3D);
g = createGraphics(width, height, P2D);
g.beginDraw();
g.background(0, 255, 0);
g.endDraw();
img = g.get();
}
void draw() {
background(img);
}Additional context
Either workaround suggests a solution. I'm happy to make a PR to fix this, you can assign it to me.
Would you like to work on the issue?
Yes, I’d like to help with this
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity
catilac commentedon Jul 13, 2026
Thanks @hx2A! Assigned to you.
hx2A commentedon Jul 15, 2026
Thank you @catilac ! This bug fix is pretty straightforward and I'll get it done.
catilac commentedon Jul 15, 2026
thanks @hx2A really appreciate your help :)
[-]`background()` method throws `NullPointerException` for `PGraphics` objects[/-][+]`set()` method throws `NullPointerException` for `PGraphicsOpenGL` objects[/+]hx2A commentedon Jul 31, 2026
Renaming this because I see
void backgroundImpl(PImage image)callsset(0, 0, image), and I can reproduce the bug withset(0, 0, g)also. And this bug only applies toPGraphicsOpenGLinstances.hx2A commentedon Jul 31, 2026
I created a PR for this, it is running the checks.
#1557
Here is test code:
hx2A commentedon Jul 31, 2026
Other permutations of renderer type or graphics type don't experience the problem. The bug only appears when both the Sketch and the graphics object are
PGraphicsOpenGLinstances. The bug is triggered withbackground()orset().hx2A commentedon Jul 31, 2026
Awesome, thank you for merging @catilac
Next up, #1422
fix processing#1520 (processing#1557)