# Why is PGraphics.g2 null

**URL:** <https://discourse.processing.org/t/why-is-pgraphics-g2-null/36623>\
**Category:** Beginners\
**Created:** [April 27, 2022, 10:27am UTC](https://discourse.processing.org/t/why-is-pgraphics-g2-null/36623 "2022-04-27T10:27:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rico](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@Rico](https://discourse.processing.org/u/Rico)\
**Post date:** [April 27, 2022, 10:27am UTC](https://discourse.processing.org/t/why-is-pgraphics-g2-null/36623/1 "2022-04-27T10:27:08Z")

</div>

I’m having trouble rendering PShapes into a PGraphics object. Here is the simplest version of the code that demonstrates the issue:

```auto
PGraphics g;

void setup() {
  size(850, 850);
  g = createGraphics(800, 800);
  PShape s = createShape(RECT, 100,100,500,500);
  g.shape(s);
}

void draw() {
  background(250);
  image(g, 25, 25);
}

```

Running this code produces the following error:  
`java.lang.NullPointerException: Cannot invoke "java.awt.Graphics2D.setStroke(java.awt.Stroke)" because "this.g2" is null`

Why is this happening please?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 27, 2022, 11:08am UTC](https://discourse.processing.org/t/why-is-pgraphics-g2-null/36623/2 "2022-04-27T11:08:59Z")

</div>

The problem is because you didn’t mark the start and end of draw for the PGraphics. This is the way to do it

```auto
PGraphics g;

void setup() {
  size(850, 850);
  
  g = createGraphics(800, 800);
  g.beginDraw();
  PShape s = createShape(RECT, 100,100,500,500);
  g.shape(s);
  g.endDraw();
}

void draw() {
  background(250);
  image(g, 25, 25);
}

```

Also I suggest that you use `pg` instead of `g` as PApplet has an attribute of the same name, at least it used to 😀

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 27, 2022, 11:12am UTC](https://discourse.processing.org/t/why-is-pgraphics-g2-null/36623/3 "2022-04-27T11:12:21Z")

</div>

Hello @Rico,

Some references related to this:

- [https://processing.org/reference/PGraphics.html](https://processing.org/reference/PGraphics.html)
- [Render Techniques / Processing.org](https://processing.org/tutorials/rendering)

`:)`

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 27, 2022, 11:18am UTC](https://discourse.processing.org/t/why-is-pgraphics-g2-null/36623/4 "2022-04-27T11:18:47Z")

</div>

@quark was faster! 🏃‍♂️ But here’s my take on it: 😛

```auto
PGraphics pg;
PShape ps;

void setup() {
  size(800, 600);
  imageMode(CENTER);
  noLoop();

  fill(#0000FF); // blue
  stroke(#FFFF00); // yellow
  strokeWeight(3.5);

  pg = createGraphics(width >> 1, height >> 1);
  ps = createShape(RECT, 50, 50, pg.width - 100, pg.height - 100);

  pg.beginDraw();
  pg.background(#FF0000); // red
  pg.shape(ps);
  pg.endDraw();
}

void draw() {
  background(#008000); // lime
  image(pg, width >> 1, height >> 1);
}

```

---

<div class="post-metadata">

**Author:** ![Rico](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@Rico](https://discourse.processing.org/u/Rico)\
**Post date:** [April 27, 2022, 1:49pm UTC](https://discourse.processing.org/t/why-is-pgraphics-g2-null/36623/5 "2022-04-27T13:49:11Z")

</div>

Thanks all, I’ve marked GoToLoop’s as the solution, but Quark’s wad just as good.
