# Unable to load fonts using createFont()

**URL:** <https://discourse.processing.org/t/unable-to-load-fonts-using-createfont/23666>\
**Category:** Coding Questions\
**Created:** [September 3, 2020, 10:52pm UTC](https://discourse.processing.org/t/unable-to-load-fonts-using-createfont/23666 "2020-09-03T22:52:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucianoinfanti](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucianoinfanti/32/10519_2.png) [@Lucianoinfanti](https://discourse.processing.org/u/Lucianoinfanti)\
**Post date:** [September 3, 2020, 10:52pm UTC](https://discourse.processing.org/t/unable-to-load-fonts-using-createfont/23666/1 "2020-09-03T22:52:45Z")

</div>

I tried to load a few different fonts I have inside my data folder but to no success. This is my code:

```auto
PGraphics pg;

void setup() {
  PFont font = createFont("ZapfDingbats.ttf", 100);
  size(800,800, P2D);
  pg = createGraphics(1080,1080, P2D);
}

```

I’ve used the following snippet to access all available fonts:

```auto
String[] fontList = PFont.list();
printArray(fontList);

```

The things I tried so far:

- Write `PFont font;` outside `setup()` and only then call `font = createFont();` inside `setup()`;
- Using .ttc, .otf and .ttf font formats;
- Calling `PFont font = createFont("ZapfDingbats.ttf", 600); outside setup()` - but the code won’t compile;
- Tried only using fonts from the available list;
- Tried both with and without the extension: `createFont("ZapfDingbats", 100);` and `createFont("ZapfDingbats.ttf");`
- Other fonts like `font = createFont("Georgia", 600);`

Any ideas of what could be happening here?

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [September 4, 2020, 7:48am UTC](https://discourse.processing.org/t/unable-to-load-fonts-using-createfont/23666/2 "2020-09-04T07:48:55Z")

</div>

Hi @Lucianoinfanti

I don’t know where you are accessing the font you are using, but try this code. It works for me.  
Also, make sure that the name of the font is the exact same. It is case sensitive.

```auto
PGraphics pg;
PFont font;

void setup() {
 font = createFont("ZapfDingbats.ttf", 100);
 size(800,800, P2D);
 pg = createGraphics(1080,1080, P2D);
}

void draw(){
background(0);
textFont(font);
textAlign(CENTER,CENTER);
text("Here it is",width/2,height/2); 
}

```

Result:

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

---

<div class="post-metadata">

**Author:** ![Lucianoinfanti](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucianoinfanti/32/10519_2.png) [@Lucianoinfanti](https://discourse.processing.org/u/Lucianoinfanti)\
**Post date:** [September 4, 2020, 11:58am UTC](https://discourse.processing.org/t/unable-to-load-fonts-using-createfont/23666/3 "2020-09-04T11:58:16Z")

</div>

Hey @MiguelSanches, thanks for replying!

Unfortunately whenever I try this code I get this error message:

> A null PFont was passed to textFont().

But I did manage to make it work. I guess the problem was related to the variable’s scope. Here is the final code:

```auto

PFont font; 
PGraphics pg;

void setup() {
  font = createFont("Soulcraft.ttf", 1000);
  size(800,800, P2D);
  pg = createGraphics(1080,1080, P2D);
  
  // Printing the Font List to understand what fonts ara avaliable for use
  //String[] fontList = PFont.list();
  // printArray(fontList);
}

void draw (){
  background(0);
  
  pg.beginDraw();
  pg.background(0);
  pg.textFont(font);
  pg.fill(250,250,250, 1000);
  pg.textSize(400);
  pg.pushMatrix();
  pg.translate(width/2, height/2-90);
  pg.textAlign(CENTER,CENTER);
  pg.text("01", 0,0);
  pg.popMatrix();
  pg.endDraw();

  image(pg,0,0);
  
  int tilesX = 8;
  int tilesY = 16;

  int tileW = int(width/tilesX);
  int tileH = int(height/tilesY);
  
  for (int y = 0; y < tilesY; y++){
    for (int x = 0; x < tilesX; x++){
      
      // WARP
      int movex = int(sin(frameCount * 0.08 + (x * y) * 0.04) * 100);
      int movey = int(cos(frameCount * 0.05 + (x * y) * 0.08) * 100);
      
      //SOURCE
      int sx = x*tileW + movex;
      int sy = y*tileH + movey;
      int sw = tileW + (movey / 2);
      int sh = tileH ;
      
      //DESTINATION
      int dx = x * tileW;
      int dy = y * tileH;
      int dw = tileW;
      int dh = tileH ;
      
      copy (pg, sx, sy, sw, sh, dx, dy, dw, dh);
    }
  }
}

```

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [September 4, 2020, 1:03pm UTC](https://discourse.processing.org/t/unable-to-load-fonts-using-createfont/23666/4 "2020-09-04T13:03:22Z")

</div>

Great @Lucianoinfanti

I believe that the null PFont error either happens because the name that you are passing when creating a font does not match the exact same that you have in the folder, including the type .ttf or . TTF or because you didn’t the variable scoped as global, which was your case I believe.
