# Why does my code give NullPointerException error?

**URL:** https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415
**Category:** Beginners
**Created:** [July 14, 2023, 1:52am UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415 "2023-07-14T01:52:52Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![prathmesh](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@prathmesh](https://discourse.processing.org/u/prathmesh)
#### Post date: [July 14, 2023, 1:52am UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/1 "2023-07-14T01:52:52Z")

</div>

Namaste all,

Why does the floowing code give a null point exception error? Processing highlights the line where I have called the line function. This is a p5js code which I am trying to recreate in processing.

Thanks.

```auto
void setup() {
  size(500, 500);
  colorMode(HSB, 360, 100, 100, 100);
  PGraphics tex = createGraphics(width, height);
  tex.colorMode(HSB, 360, 100, 100, 100);
  
  for (int i = 0; i < (width * height) / 100; i++) {
    if (random(360) < 0.5) {
      tex.stroke(0, 0, 100, 2);
    } else {
      tex.stroke(0, 0, 0, 1);
    }
    float cx1 = random(width);
    float cy1 = random(height);
    float angle = random(360);
  
    tex.line(cx1 + sin(radians(angle)) * width, cy1 + cos(radians(angle)) * height, cx1 + sin(radians(angle + 180)) * width, cy1 + cos(radians(angle + 180)) * height);
  }
}

void draw(){
}

```

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [July 14, 2023, 4:55am UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/2 "2023-07-14T04:55:59Z")

</div>

Try using beginDraw() and endDraw(). Reference is here: [https://processing.org/reference/PGraphics.html](https://processing.org/reference/PGraphics.html)

Will also need a global variable: PGraphics tex;

Call the PGraphics in draw() with image(tex, 0, 0);

_[Code removed]_

---

<div class="post-metadata">

### Author: ![prathmesh](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@prathmesh](https://discourse.processing.org/u/prathmesh)
#### Post date: [July 17, 2023, 4:54pm UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/3 "2023-07-17T16:54:52Z")

</div>

Thanks for the help 😃

---

<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: [July 17, 2023, 5:10pm UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/4 "2023-07-17T17:10:49Z")

</div>

> The code included in this post has been taken out because it completely solved the problem,

Only actual things @svan did was globally declaring variable _tex_ w/ `PGraphics tex;`, plus adding `tex.beginDraw();` & `tex.endDraw();` to the OP’s already provided code!

> , instead of just providing pre-made answers.

There’s nothing to provide but link to very basic requirements for dealing w/ [PGraphics](https://processing.org/reference/PGraphics.html) objects.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [July 18, 2023, 2:25am UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/5 "2023-07-18T02:25:35Z")

</div>

`Thanks for the help`

You’re quite welcome; we are always happy to help with coding issues. Good luck with your project.

---

<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: [July 18, 2023, 3:18am UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/6 "2023-07-18T03:18:16Z")

</div>

Hello @prathmesh,

There are lots of resources (tutorials, references, examples, etc.) here:

> **[Welcome to Processing!](https://processing.org/)**
>
> Processing is a flexible software sketchbook and a language for learning how to code. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology…

Related to your topic:

- [Render Techniques / Processing.org](https://processing.org/tutorials/rendering)
- [PGraphics / Reference / Processing.org](https://processing.org/reference/PGraphics.html) \< Check out the related ones on this page also!

Be careful to scrutinize code carefully and learn from it.

The code that was removed had the _tex.endDraw()_ inside the loop which may not yield the intended or desired results.  
You can try it inside the loop and outside of the loop to see the difference.

Have fun!

`:)`

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [July 18, 2023, 3:51am UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/7 "2023-07-18T03:51:32Z")

</div>

> [@glv](#):
>
> The code that was removed had the _tex.endDraw()_ inside the loop which may not yield the intended or desired results.

How do you know that?? It’s been removed. I seriously doubt that I would have posted code which did not run.

---

<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: [July 18, 2023, 3:54am UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/8 "2023-07-18T03:54:53Z")

</div>

> [@svan](#):
>
> It’s been removed.

We can view the history of edits via the pencil ✏ icon. 😉

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [July 18, 2023, 3:57am UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/9 "2023-07-18T03:57:23Z")

</div>

The initial post was not edited to the best of my knowledge. It was only edited after someone deleted the code and thereby changed the answer.

---

<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: [July 18, 2023, 4:01am UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/10 "2023-07-18T04:01:30Z")

</div>

Excerpt of the original post before any moderation edits:

```auto
    tex.line(cx1 + sin(radians(angle)) * width, cy1 + cos(radians(angle)) * height, cx1 + sin(radians(angle + 180)) * width, cy1 + cos(radians(angle + 180)) * height);
    tex.endDraw();
  }
}

```

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [July 18, 2023, 4:15am UTC](https://discourse.processing.org/t/why-does-my-code-give-nullpointerexception-error/42415/11 "2023-07-18T04:15:59Z")

</div>

The code does run either way, but the output is markedly different. Apparently I did not keep a copy and just deleted it after I solved the null pointer error, so it is possible it was initially inside the for loop when obviously it should be outside. I always assumed I could always go back to the original post and see my code, but obviously that is not the case. Good to know that some of us have more editorial tools than others, and some have the power to just wipe away an answer.
