Null pointer error in thread

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

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);
    //}
    }
  };
};

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.

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

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

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

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.

Does that mean I should draw create another PGraphics instance?

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

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);
  }

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

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

What goes inside your own post() draw() is up to you. :pen:

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

Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#registerMethod-java.lang.String-java.lang.Object-

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?! :confused:

His callback post() draw() would be the only part of his threaded code that’s gonna be delegated to the “Animation” Thread. :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. :bulb:

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

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.

  • 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.
  public void run() {
    while (true) {
      count++;
      displayNumbers();
      try {
        Thread.sleep(100); // milliseconds
      }
      catch(InterruptedException e){
      }
    }
  }

You cannot use the post() method to directly render to the main display as it is executed after the draw() method has finished rendering.

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

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.

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.

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

Or stick w/ registerMethod() you class’ draw() callback, which is OK too: :man_shrugging:

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

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!