# Null pointer error in thread

**URL:** https://discourse.processing.org/t/null-pointer-error-in-thread/21499
**Category:** Coding Questions
**Created:** [June 2, 2020, 8:51am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499 "2020-06-02T08:51:11Z")
**Posts on this page:** 10
**Page:** 3

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 2, 2020, 12:08pm UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/41 "2020-06-02T12:08:17Z")

</div>

This by far is probably the easiest solution and provides a 2x increase in performance allowing me to draw a grid twice the size as before at 60 fps. Its not multithreading but it is better performance.

---

<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: [June 2, 2020, 12:17pm UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/42 "2020-06-02T12:17:35Z")

</div>

> [@paulgoux](#):
>
> ```auto
> void run() {
> registerMethod("draw", applet);
> };
> 
> ```

What is that _applet_ argument you’re passing to **registerMethod()**?

You’ve gotta use your class’ reference so it calls back your class’ **draw()** method!

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 2, 2020, 12:22pm UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/43 "2020-06-02T12:22:48Z")

</div>

ooh ignore that, I thought it had something to do with the threads not being synchronized to the main render window, and thought linking and passing the main `PApplet` would help. It did not, assume the line to use `this`,. The code at the moment, more often than not just produces a gray box the size of the canvas, restart it, another gray box, restart it again, and the background is displayed, this is completely random and was reminding me of how the threads were just called randomly, so in my limited knowledge I thought pass `PApplet`. In any case, nothing but the background is drawn when something does happen.

---

<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: [June 2, 2020, 12:29pm UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/44 "2020-06-02T12:29:35Z")

</div>

Let me explain what the **registerMethod()** approach is suppose to achieve.

Your class A runs on its own Thread, but you can’t draw/mutate the sketch’s main canvas from it.

By using **registerMethod()** you can forward class A’s rendering parts to the sketch’s “Animation” Thread, leaving to class A’s Thread only the parts which isn’t about drawing.

As a workaround, you can have a separate PGraphics’s inside your class A, and leave to its registered customized callback **draw()** the task to display it.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 2, 2020, 12:29pm UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/45 "2020-06-02T12:29:58Z")

</div>

out of curiosity, using the `PApplet` method do all the applet remain synchronised?

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 2, 2020, 12:39pm UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/46 "2020-06-02T12:39:04Z")

</div>

Ok gotcha so it can only accept regular canvas commands and nothing else. At least from what I understand.

if I change the code to the following

```auto
public void draw(){
    background(50);
    fill(0);
    ellipse(0,0,width,height);
    //display();
      fill(255);
    text("hello",50,50);
    //System.out.println(count);
  };

```

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/de5cf1e1724e4d988380aa4bd63bc61fcab10091.png)

however add anything else and it just goes wonky.

---

<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: [June 2, 2020, 12:39pm UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/47 "2020-06-02T12:39:47Z")

</div>

> [@paulgoux](#):
>
> , using the PApplet method do all the _applet_ remain synchronized?

PApplet is a class, so I don’t know which method you’re talking about.

Regardless, threads are responsible to execute all the code it passes through.

The matter here is that the way Processing library is programmed, its main canvas is exclusive to its “Animation” Thread to touch it.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 2, 2020, 12:41pm UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/48 "2020-06-02T12:41:25Z")

</div>

😅

I meant the PApplett approach not method…

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [June 2, 2020, 12:45pm UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/49 "2020-06-02T12:45:23Z")

</div>

Thank-you to everyone for your contributions, I shall test all the methods suggested and post the various solutions later with and see which one performs the best.

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [September 19, 2020, 2:53am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/50 "2020-09-19T02:53:57Z")

</div>

Bit of a while since, I have been busy with various projects, but I just wanted to post back with a solution that I was happy with.

This solution makes no use of multi-threading and having looked into in it should provide some speed improvements when generating the grid itself.

Currently the solution I implemented was to use canvas and a PImage to store the perlin grid.  
When update is on the grid draws all tiles to the canvas, just once then once completed the get() function is used to grab the canvas image. This produces something which is able to run 60fps no problem along all renderers, when fully loaded, and runs at around 20fps on FX2D when loading which is still acceptable.  
This new method also allows greater resolution and size for the map;.

Also as per Sebastian Lagues suggestion in his procedural generation playlist, I have added frequency amplitute, octaves, lacunarity and persistance to increase the diversity that one can create with the grid.

Still 2d at the moment, but in combination with a sobel operator we can now have edges useable in real time withought any performance issues as the are once again calculated only once.

I shall hopefully try to complete this to add the other things from Sebastians playlist, eg chunks, shaders etc.

[https://www.youtube.com/results?search\_query=sebastian+lague+procedural](https://www.youtube.com/results?search_query=sebastian+lague+procedural)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fa751a5939df8b2537c7658ab2f434b8d882ce82.png)

[Previous page](https://discourse.processing.org/t/null-pointer-error-in-thread/21499.md?page=2)
