# \[SOLVED\] PGraphics.image gives NullPointerException

**URL:** <https://discourse.processing.org/t/solved-pgraphics-image-gives-nullpointerexception/8076>\
**Category:** Coding Questions\
**Created:** [February 3, 2019, 5:53pm UTC](https://discourse.processing.org/t/solved-pgraphics-image-gives-nullpointerexception/8076 "2019-02-03T17:53:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 3, 2019, 5:53pm UTC](https://discourse.processing.org/t/solved-pgraphics-image-gives-nullpointerexception/8076/1 "2019-02-03T17:53:48Z")

</div>

I created a sketch with a PImage that I was then going to draw into a PGraphics using PGraphics.image() – but I got a NullPointerException.

Why is this happening?

I have created an MCVE below to demonstrate the problem on Processing 3.4:

```auto
PImage img;
PGraphics pg;

void setup() {
  img = loadImage("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/Processing_3_logo.png/240px-Processing_3_logo.png");
  pg = createGraphics(240, 240);
  pg.image(img, 0, 0); // NullPointerException
}

void draw(){
  image(pg, 0, 0);
  noLoop();
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 3, 2019, 6:36pm UTC](https://discourse.processing.org/t/solved-pgraphics-image-gives-nullpointerexception/8076/2 "2019-02-03T18:36:22Z")

</div>

Sigh. So simple – I forgot to call `beginDraw()` / `endDraw()` before and after `pg.image()`, so it wasn’t accessing `pixels[]` correctly to draw.

The PImage and PGraphics both existed, but beginDraw “should be called before anything is drawn into the object.”

[https://processing.org/reference/PGraphics\_beginDraw\_.html](https://processing.org/reference/PGraphics_beginDraw_.html)

I periodically forget this when I haven’t used a PGraphics in a while – just like I periodically forget about loadPixels.

Sad trombone.

But problem solved.
