Creating a font in setup but getting error in draw that no font are loaded

Hi !
I’m using this code to generate a font that and use it in my setup function :

public void setup() {
    PFont font = createFont("Arial", 32);
    textFont(font);
}

And on my draw function I just do

public void draw() {
    text("test ?", 10, 10);
};

but I’m getting this error :

java.lang.RuntimeException: Use textFont() before text()

I have a lot of code in these functions but nothing is using text() before the setup function, the only text that I’m trying to draw is in my draw function so it can’t be the cause right ?

So what am I doing wrong ?

1 Like

Hi,

I run the following sketch and it worked for me. Please re-check your code or post its full version for someone trying to help you

public void setup() {
  size(120,120);
    PFont font = createFont("Arial", 32);
    textFont(font);
}

public void draw() {
  background(0);
    text("test ?", 20, 60);
}

Best regards

1 Like

There are several reasons why this might happen, but it would help if you copy the full error message (might be several lines of text) shown in the console pane here.

I found the reason, it was pretty stupid, the text that I tried to draw was in a PGraphics, and I found that we have to create the found into the PGraphics and not the main sketch :octopus:

1 Like

I have just been looking at the source code and this exception is thrown when PGraphics (or child class) does not have a parent PApplet. This agrees with what you found.

2 Likes