# \[SOLVED\] Setting PGraphics background in setup() causes NullPointerException?

**URL:** <https://discourse.processing.org/t/solved-setting-pgraphics-background-in-setup-causes-nullpointerexception/3736>\
**Category:** Coding Questions\
**Created:** [September 22, 2018, 1:30pm UTC](https://discourse.processing.org/t/solved-setting-pgraphics-background-in-setup-causes-nullpointerexception/3736 "2018-09-22T13:30:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaronshenhao](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@aaronshenhao](https://discourse.processing.org/u/aaronshenhao)\
**Post date:** [September 22, 2018, 1:30pm UTC](https://discourse.processing.org/t/solved-setting-pgraphics-background-in-setup-causes-nullpointerexception/3736/1 "2018-09-22T13:30:11Z")

</div>

I’m using the Java version for Processing and I’ve been following a tutorial on PGraphics which uses p5.js. At one point, they initialize the background for the PGraphics layer in setup. However, when I try it, it produces a null pointer error. I also tried using the P2D renderer, but it doesn’t solve the problem. Here’s a simplified example showing the problem:

```auto
PGraphics pg;

void setup() {
  size(600, 400);
  pg = createGraphics(600, 400);
  pg.background(0);
}

```

This doesn’t make any sense to me, since I’m pretty sure I’ve copied the code exactly, even though the example was in p5.js. Did I miss something obvious? Thanks for the help.

---

<div class="post-metadata">

**Author:** ![aaronshenhao](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@aaronshenhao](https://discourse.processing.org/u/aaronshenhao)\
**Post date:** [September 22, 2018, 1:35pm UTC](https://discourse.processing.org/t/solved-setting-pgraphics-background-in-setup-causes-nullpointerexception/3736/2 "2018-09-22T13:35:00Z")

</div>

Ooops, 5 minutes after I submitted this, I realized that the js version doesn’t seem to need to use beginDraw() and endDraw(). Works fine now 😬,
