# 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:** 20
**Page:** 1

<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, 8:51am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/1 "2020-06-02T08:51:11Z")

</div>

I’m trying to implement threading, however anything that is called on the canvas results in a nullpointer error.

```auto
String [] messages = {"Test string for secondary thread",
                      "Here's another message.",
                      "And this is yet another thread."};
                      
int [] numbers = { 0,1,2,3,4,5,6,7,8,9,10};
int threads = 1;
                      
void setup(){
  size(200,200);
  for(int i=0;i<threads;i++){
    
    new Thread(new A(numbers)).start();
  }
};

void draw(){
  background(0);
  fill(255);
  text(frameRate,100,100);
};

class A implements Runnable {
  String to_print;
  int t,count = 0;;
  boolean ints,intss,strings;
  int [] numbers = new int[1];
  String [] messages = new String[1];
  
  A(String to_print) {
    this.to_print = to_print;
  };
  
  A(Integer t) {
    this.t = t;
    intss = true;
  };
  
  A(String [] messages) {
    this.messages = messages;
    strings = true;
  };
  
  A(int [] numbers) {
    this.numbers = numbers;
    ints = true;
  };
  
  public void run() {
    while(true){
      count++;
    //if(ints)System.out.println(count);
    //displayMessages();
    displayNumbers();
  }
  };
  
  void displayMessages(){
    if(strings)
    for(int i=0;i<messages.length;i++){
      
    String message = messages[i];
    
    fill(255);
    text(message,100,100);
    }
  };
  
  void displayNumbers(){
    if(ints){
    //text(count,100,100);
    fill(255);
    rect(10,10,width-20,height-20);
    //System.out.println(count);
    //for(int i=0;i<numbers.length;i++){
      
    //int number = numbers[i];
    
    //fill(255);
    //text(numbers.length,100,100);
    //}
    }
  };
};

```

---

<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: [June 2, 2020, 8:57am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/2 "2020-06-02T08:57:24Z")

</div>

The problem is that you are trying to draw to the display from a secondary thread. All rendering _ **must** _ be done in the main event thread.

---

<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, 8:58am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/3 "2020-06-02T08:58:38Z")

</div>

I don’t quite understand, all this is rather new to me, and anything called to the console works fine…

---

<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, 9:05am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/4 "2020-06-02T09:05:32Z")

</div>

Implement a method named **post()** **draw()** inside your class A, and then **registerMethod()** it: 👾

> [@Fisica's FGearJoint](https://discourse.processing.org/t/fisicas-fgearjoint/18037/2):
>
> I’ve taken a quick look at Fisica’s source code and found out the reason for the NPE is that invoking method getBox2dJoint() on both FRevoluteJoint objects return null: println(revJointA.getBox2dJoint()); // null println(revJointB.getBox2dJoint()); // null But mysteriously after the 1st draw() iteration, those same statements don’t log null anymore. So I’ve used registerMethod() in order to setup a post() callback which would be run once only and then got unregisterMethod(): [Processing.GitHu…](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#registerMethod-java.lang.String-java.lang.Object-)

---

<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, 9:10am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/5 "2020-06-02T09:10:40Z")

</div>

> [@GoToLoop](#):
>
> registerMethod

Thanks for all your help guys, I shall do a bit more digging.

---

<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: [June 2, 2020, 9:14am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/6 "2020-06-02T09:14:01Z")

</div>

When a Processing sketch is running there is a _ **single thread** _ that handles all events (mouse click, mouse move, key pressed etc) and rendering the display(i.e. the `draw` method).

In your code you create a second thread and from that you attempt to render to the display e.g.  
`rect(10,10,width-20,height-20);`

this is not permitted. It restriction imposed by the JVM (Java Virtual Machine) which actually executes your program.

The console is different and is thread safe.

---

<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, 9:16am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/7 "2020-06-02T09:16:35Z")

</div>

Does that mean I should draw create another PGraphics instance?

---

<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, 9:22am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/8 "2020-06-02T09:22:00Z")

</div>

In the following example you provided a while back you did not need to do this, is this because only one thread is called?

```auto
unregisterMethod("post", this);

  println(revJointA.getBox2dJoint());
  println(revJointB.getBox2dJoint());

  // Connecting the two Joints ... //////////////////////////////////////
  FGearJoint gearJoint = new FGearJoint(revJointA, revJointB);
  gearJoint.setRatio(2);
  world.add(gearJoint);
  ////////////////////////////////////////////////////////////////////////

  try {
    world.step();
  }

  catch (final AssertionError err) {
    System.err.println(err);
  }

```

---

<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, 9:26am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/9 "2020-06-02T09:26:38Z")

</div>

Can post be embedded in a class or does it have to be a standalone function?

---

<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, 9:28am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/10 "2020-06-02T09:28:03Z")

</div>

That’s a very particular example that’s got nothing to do w/ your case, sorry. 😑

What goes inside your own **post()** **draw()** is up to you. 🖊

Also, you’ll need to use **registerMethod()** inside your class, not inside **setup()**: 🧐

[Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#registerMethod-java.lang.String-java.lang.Object-](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#registerMethod-java.lang.String-java.lang.Object-)

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [June 2, 2020, 9:31am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/11 "2020-06-02T09:31:59Z")

</div>

> [@paulgoux](#):
>
> I don’t quite understand, all this is rather new to me,

Take a step back, here. What are you actually trying to achieve? Threading, particularly at the lowest level managing the threads yourself, is advanced level, and probably isn’t something to consider if it’s all new to you.

I have no idea how the `post()` suggestion is meant to help you?! 😕

---

<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, 9:34am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/12 "2020-06-02T09:34:25Z")

</div>

> [@neilcsmith](#):
>
> I have no idea how the **post()** suggestion is meant to help you?!

His callback **post()** **draw()** would be the only part of his threaded code that’s gonna be delegated to the “Animation” Thread. 🧵

A full-blown option would be to have an invisible PApplet to draw stuff, then **arrayCopy()** it to some PImage, so the main PApplet can render it. 💡

A lighter 1 would be to draw on a separate PGraphics, but only **image()** it under the “Animation” Thread.

---

<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, 9:35am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/13 "2020-06-02T09:35:03Z")

</div>

Thats alright, are you referring to your first post or your previous example I posted afterwards? Just going through the info in the link now.

---

<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: [June 2, 2020, 9:36am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/14 "2020-06-02T09:36:09Z")

</div>

- So you would create an off-screen PGraphics object
- Update the off-screen graphics in the secondary thread
- In draw() use image(…) to display the graphic.

That would be a good way to do it but there are some caveats

- Would need a mechanism to prevent draw() attempting to display the graphic at the same time the other thread is updating it. (I would probably use a double buffered image)
- In the secondary thread inside the run() method you need to put a delay in the while loop to prevent it hogging the CPU like this.

```auto
  public void run() {
    while (true) {
      count++;
      displayNumbers();
      try {
        Thread.sleep(100); // milliseconds
      }
      catch(InterruptedException e){
      }
    }
  }
```

---

<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: [June 2, 2020, 9:40am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/15 "2020-06-02T09:40:24Z")

</div>

You cannot use the `post()` method to directly render to the main display as it is executed after the draw() method has finished 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: [June 2, 2020, 9:42am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/16 "2020-06-02T09:42:58Z")

</div>

Oh, you’re right! We need to **registerMethod()** “draw” instead of “post”. My bad! 🥴

---

<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, 9:48am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/17 "2020-06-02T09:48:12Z")

</div>

I’m creating a program at the moment using processing however I keep running into cpu bottlenecks, because processing only makes use of 1 core.

---

<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, 9:55am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/18 "2020-06-02T09:55:28Z")

</div>

I guess that would be an ok solution however there is currently a problem in processing where PGraphics cannot be instanced with FX2D which is currently my fave renderer due to the speed advantage over other renderers, I am open to being persuaded to use an alternative, in which case this could work.

---

<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, 9:57am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/19 "2020-06-02T09:57:33Z")

</div>

> [@paulgoux](#):
>
> I am open to being persuaded to use an alternative, in which case this could work.

Go w/ my full-blown threaded alternative idea, so you can use FX2D there: 😉

> [@GoToLoop](#):
>
> A full-blown option would be to have an invisible PApplet to draw stuff, then **arrayCopy()** it to some PImage, so the main PApplet can render it. 💡

Or stick w/ **registerMethod()** you class’ **draw()** callback, which is OK too: 🤷‍♂️

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [June 2, 2020, 9:59am UTC](https://discourse.processing.org/t/null-pointer-error-in-thread/21499/20 "2020-06-02T09:59:07Z")

</div>

What is it doing though? I assume this isn’t the code you’re talking about? If it’s mainly drawing things, then maxing out a single core isn’t going to be fixed by this. All the renderers are single threaded, so you need to consider a more powerful renderer.

eg. OpenGL

```auto
size(200,200,P2D);

```

On the other hand, if it’s maxing out in non-drawing calculations, then you might get a benefit from threading. Although as @quark points out, a loop without some sort of sleep or other call that causes it to pause for a while will just max out the CPU core for no reason.

I have no idea how @GoToLoop idea is meant to help you. The best way to pass data from one thread into the animation thread is through using one of the blocking queues available inside the core Java libraries. For some reason, Processing people decided not to provide a beginner friendly utility around this. Threading must be the single biggest cause of confusion around here!

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