PGraphics - can't get text to display

DDJDDJ
edited December 2015 in Questions about Code

Hi,

I am working with a PGraphics item to display an information panel on screen, but can't get it to display text. The code is below.

As it stands currently there is a runtime Null Pointer Exception at the point shown below.

If I take all of the font references out, I get no runtime errirs, but only the rectangle is displayed, not the text.

Can anyone point out what I'm overlooking here please?

Thanks.

final int TEST_TEXT_X1=12;
final int TEST_TEXT_X2=80;
final int TEST_BOX_X=10;
final int TEST_BOX_Y=320;
final int TEST_BOX_HEIGHT=79;
final int TEST_BOX_WIDTH=100;
final int TEST_TEXT_GAP=17;
final int TEST_DATA_Y1=TEST_BOX_Y+15;
final int TEST_DATA_Y2=TEST_DATA_Y1+TEST_TEXT_GAP;
final int TEST_DATA_Y3=TEST_DATA_Y2+TEST_TEXT_GAP;
final int TEST_DATA_Y4=TEST_DATA_Y3+TEST_TEXT_GAP;
final int TEST_DATA_Y5=TEST_DATA_Y4+TEST_TEXT_GAP;
final int TEST_CLEAR_X=40;
final int TEST_CLEAR_Y=TEST_BOX_Y+2;
final int TEST_CLEAR_HEIGHT=50;
final int TEST_CLEAR_WIDTH=90;
//
PGraphics testPanel; // panel to display test information
//
void setup() {
  colorMode(RGB);
  size(1400, 500);
  background(255,255,255);  
  // If I remove the font stuff from here.....
  PFont font;
  font=loadFont("Calibri-20.vlw");
  testPanel.textFont(font,12);  // ********* Null Pointer Exception here ********
  // ....to here no runtime error but no text displays either
  testPanel = createGraphics(TEST_BOX_HEIGHT+2, TEST_BOX_WIDTH+2);
  testPanel.beginDraw();
  testPanel.stroke(0,0,0);
  testPanel.strokeWeight(1);
  testPanel.fill(255,255,255);
  testPanel.rect(0, 0, TEST_BOX_HEIGHT, TEST_BOX_WIDTH);
  testPanel.fill(0,0,0);
  testPanel.textSize(12);
  testPanel.text("A", TEST_TEXT_X1, TEST_DATA_Y1);
  testPanel.text("B", TEST_TEXT_X1, TEST_DATA_Y2);
  testPanel.text("C:", TEST_TEXT_X1, TEST_DATA_Y3);
  testPanel.text("D", TEST_TEXT_X1, TEST_DATA_Y4);
  testPanel.text("E:", TEST_TEXT_X1, TEST_DATA_Y5);
  testPanel.endDraw();
}

void draw() {
  image(testPanel, TEST_BOX_X, TEST_BOX_Y);
}

Answers

  • Answer ✓

    To format your code properly, please read this sticky thread.

    The NullPointerException is caused by using textfont() before beginDraw(). If you call it after you call beginDraw() it should work.

    The reason for your text not beeing visible, is that you draw it outside of your PGraphics-area. You set TEST_DATA_Y1 to 335, while your PGraphics has only a height of 81 pixels.

  • edited December 2015

    The NullPointerException is caused by using textfont() before beginDraw().

    Actually it's primarily caused by the fact variable testPanel is still null before createGraphics()! 8-X

  • Thanks for the answers - problem solved and hopefully learning embedded!

  • edited December 2015

    @DDJ Please edit the original post to format you code for this forum.

    In this case
    delete the reverse-apostrophes at the start and end of code
    make sure there is a blank line before and after the code
    highlight the entire code and press Ctrl-O (Cmd-O on OSX) which will indent the code block 4 spaces.
    save the changes

Sign In or Register to comment.