0

I'm trying to imitate a similar code seen here http://www.openprocessing.org/sketch/7050 but I seem to have gotten a little mixed up with the code. I'm trying to get the effect of the letters essentially drawing the posterized image of the picture. But I have getting a NullPointException and assume is has to do with how I'm initializing the string variables but i can't seem to find what I'm doing wrong.

The Error

Exception in thread "Animation Thread" java.lang.NullPointerException
    at processing.opengl.PGL.getString(PGL.java:1029)
    at processing.opengl.PGraphicsOpenGL.getGLParameters(PGraphicsOpenGL.java:6076)
    at processing.opengl.PGraphicsOpenGL.beginDraw(PGraphicsOpenGL.java:1547)
    at MLKpractice.letterfit(MLKpractice.java:147)
    at MLKpractice.draw(MLKpractice.java:98)
    at processing.core.PApplet.handleDraw(PApplet.java:2120)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:197)
    at processing.core.PApplet.run(PApplet.java:1998)
    at java.lang.Thread.run(Thread.java:680)

The program says the error is on line 127

lettersquare.beginDraw();

But I believe the error is somewhere above

PFont font;
String fontpath = "ArialMT-200.vlw";
int fontstart = 300;
int fontend = 8;
float fontsize = fontstart;
float fontsizedecrease = 0.97;
PImage bg;
PImage australia;
PImage austria;
String country1 = "australia.jpg";
String country2 = "austria.jpg";
String letters = "Australia";
char[] chars = new char[52];
int nchars = 0;
int iterations = 500;
int c = 0;
PGraphics letter,lettersquare,drawing;

void setup(){
  //initialize the sketch
  size(900,600);
  //background(255);
  //initialize the font
  //font = loadFont(fontpath);
  ///*
  for(int i=0;i<letters.length();i++){
    boolean found = false;
    char lc = letters.charAt(i);
    for(int j=0;j<nchars;j++){
      if(chars[j]==lc){
        found = true;
        break;
      }
    }
    if(!found) chars[nchars++] = lc;
  }
  chars = (char[]) subset(chars,0,nchars);
  font = createFont("Arial",200,true,chars);
  //*/
  textAlign(CENTER,CENTER);
  //load the image that will be filled with letters
  australia = loadImage(country1);
   austria = loadImage(country2);

  bg = loadImage("background.jpg");
  //posterize the image
  australia.filter(THRESHOLD,0.4);
  australia.filter(BLUR,3);
  australia.filter(THRESHOLD,0.6);
  //initialize the drawing buffers
  letter = createGraphics((int)fontsize,(int)fontsize,JAVA2D);
  lettersquare = createGraphics((int)fontsize,(int)fontsize,P2D);
  drawing = createGraphics(width,height,JAVA2D);
  drawing.beginDraw();
  drawing.background(255);
  // THIS STUPID THING NEEDS TO GO HERE!!!!
  drawing.image(bg,0,0);
  drawing.endDraw();
}

void draw(){

  if(floor(fontsize)>fontend&&c<letters.length()-1){
    if(!letterfit()){
      fontsize *= fontsizedecrease;
    }else{
      c++;
      if(c==11){
        fontsize *= 0.75;
      }
    }

    tint(255);
    image(drawing,0,0);


  if (keyCode == LEFT) {
    image(austria,0,0);
    }
   // if (keyCode == RIGHT) {
     // frog1.frogx = frog1.frogx + 1;
   // }


    if(mousePressed){
      tint(255,100);
      image(australia,0,0);
    }
  }else{
    tint(255);
    image(drawing,0,0);
    println(c+" "+letters.length());
    /*
    save("mlk-"+hour()+""+minute()+""+second()+".tif");
    exit();
    */
    noLoop();
  }
} 
boolean letterfit(){
  letter.beginDraw();
  letter.background(255,0);
  letter.fill(0);
  letter.textAlign(CENTER,CENTER);
  letter.translate(fontsize/2,fontsize/2);
  letter.rotate(random(TWO_PI));
  letter.scale(fontsize/fontstart);
  letter.textFont(font);
  letter.smooth();
  letter.text(letters.charAt(c),0,0);
  letter.endDraw();
  lettersquare.beginDraw();
5
  • Have you called setup() before you call letterfit()? Commented Nov 28, 2012 at 16:20
  • @JoachimIsaksson yes, I don't call letter fit until later on Commented Nov 28, 2012 at 16:27
  • I would print out the value of letter at the beginning of letterfit(). It probably is 0. Somehow! Commented Nov 28, 2012 at 16:31
  • @ChristineAustin try to print the return value from your graphicwatever call Commented Nov 28, 2012 at 16:32
  • It's just peculiar how at one point I could take the whole code from the site I'm referencing and the code worked, but now if I try doing that, Im getting the Error.... Commented Nov 28, 2012 at 16:50

1 Answer 1

2

You've most likely been hit by issue 1217, which prevents you from using an OpenGL PGraphics renderer if the main renderer is Java2D.

The link has a workaround, which basically involves changing the main renderer to OpenGL.

A newer version of PGraphics should give you a more detailed exception.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah I can see now, is this all because of Processing 2.0b1? I'm just worried cause I'm using this for an assignment and the school computers run on a previous version where my original code works.
@ChristineAustin From what I know, it's related to 2.x versions only, 1.x should work.
Alrighty, tysm for all your help! I think I am going to play it save and run it on the 1.x version instead since the code is working there. I would have never noticed this on my own! Genius :]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.