Skip to content

background() method throws NullPointerException for PGraphics objects #1520

Description

@hx2A

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
NullPointerException

Steps 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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions