Loading...
Logo
Processing Forum
Hi, I'm fairly new to Processing, and I was wondering if someone could help me figure out why I'm getting a Null Pointer Exemption. I've tried to link a Twitter feed that would spawn new life from an image with each new tweet, so it looks like the image (the letter "g") is "drawing on". Although, I'm not sure I've actually linked the Twitter feed to the moving pixels. Maybe I could get a thought about that too? Thanks so much for any help.

I'm getting this error:
Display 0 does not exist, using the default display instead.
Exception in thread "Animation Thread" java.lang.NullPointerException
at g_test_twitter.getTweet(g_test_twitter.java:63)
at g_test_twitter.draw(g_test_twitter.java:50)
at processing.core.PApplet.handleDraw(PApplet.java:1631)
at processing.core.PApplet.run(PApplet.java:1530)
at java.lang.Thread.run(Thread.java:680)

On this line of code:  return tmp.getContent();

The code works for a while, updating the image and displaying tweets, but then it stops with the error.

Here is the code I've been tweaking:

PImage img;
PFont i;  
int amount = 5000;
String searchTerm = "design";
String url = "http://search.twitter.com/search.atom?q=" + searchTerm + "&rpp=1";
Punt[] tip = new Punt[amount];

void setup() {
  size(600,600);
  smooth();
  img = loadImage("generativeg.png");


  for (int i = 0; i < amount; i++) {
  tip[i] = new Punt(int(random(width)),int(random(height)),int(random(3)));
  }
  
  for (int i = 0; i < amount; i++) {
  tip[i].begin();
  }
  
  background(255);
 // image(img,0,0,313,313);

  }
  
  void draw() {
   background(255);

   text (getTweet(), 40 , 570);
   fill(0);
   img.loadPixels();
   for(int i = 0; i < amount; i++) {
   tip[i].move();
   
  } 
    img.updatePixels();
  }
  
    String getTweet(){
XMLElement tweet = new XMLElement(this, url);
XMLElement tmp = tweet.getChild("entry/title");
return tmp.getContent();
}
  
  class Punt {
 float x, y;
 float lastx,lasty;
 float theta;
 int direction;
 int life = 0;
 float rb,gb,bb;


Punt(int X,int Y,int D) {
 x = X;
 y = Y;
 direction = D;
  theta = random(2*PI);
}

  void begin() {
    int loc = int(x) + int(y)*width;
   
     rb = red(img.pixels[loc]);
     gb = green(img.pixels[loc]);
     bb = blue(img.pixels[loc]);
  }
  
  void move() {
    life++;
    if (life > 250) {newLife();}
    if(x >= width || x <= 0 || y <= 0 || y >= height) newLife();
     int loc = int(x) + int(y)*width;
    float r = red(img.pixels[loc]);
    float g = green(img.pixels[loc]);
    float b = blue(img.pixels[loc]);
    if (r < rb-10 || r > rb+10 || g < gb-10 || g > gb+10 || b < bb-10 || b > bb+10){

     theta += random(-0.1,0.2);
   x = lastx;
   y = lasty;
    }
    else {      stroke(rb,gb,bb,96);
    strokeWeight(2);
      lastx=x;
     lasty=y;
     theta += random(-0.2,0.5);
   x += random(5)*cos(theta);
   y += random(5)*cos(theta);
     line(x,y,lastx,lasty);}
  }
  
  void newLife() {
    x = int(random(width)); y = int(random(height)); life = 0; direction = int(random(3)); begin();
  }
  
}

void mouseClicked() {
 setup(); 
}

void keyPressed()
{
  saveFrame("tweet_######.png");
}



Replies(3)

I couldn't test it, but here's my attempt:   

In case variable tweet from function  getTweet() gets a nullthere are no methods to call from it.
That is, you can't call method getContent() if variable tweet has no reference to that object's instance!

P.S.: Isn't it more appropriate to have both loadPixels() & updatePixels() inside method move()?

Perhaps you should instantiate class XMLElement once in setup().
Then just use its getChild() method w/o any need to allocate a new XMLElement() over and over!  
Copy code
    // ...
    
    void setup() {
      // ...
    
      frameRate(10);
      fill(0);
    }
    
    void draw() {
      background(-1);
    
      text(getTweet(), 40, 570);
    
      img.loadPixels();
      for ( int i = 0; i != amount; tip[i++].move() );
      img.updatePixels();
    }
    
    String getTweet() {
      XMLElement tweet = new XMLElement(this, url)
        .getChild("entry/title");
    
      return tweet == null ? "Failed!" : tweet.getContent();
    }
    

Hmm, there was another Null Point Exemption Error the first time I ran it with your corrections.
But, it's been running great ever since...about a half hour or so with no interruption...I believe that fixed it.
I can't thank you enough! Such a quick response too.

I didn't see you updated the previous post. Thanks for the pointers, I basically know what things are doing now, but I'm really trying to work on syntax and stuff. I had tried the  loadPixels() & updatePixels() inside the move() but it kept giving me the StackOverflowError: too much recursion.

Best,
F

I had tried the  loadPixels() & updatePixels() inside the move() but it kept giving me the StackOverflowError: too much recursion.
Oh, my mistake!    Since move() is inside a loop and run many times!
Anyways, all instances of class Punt use the very same PImage img object! 

An extra tip -> you have to determine how many FPS your app should run.
So it doesn't request too much from Tweeter connections in such short time! 
Control that w/ frameRate() for example.

Like I said I can't run your code. So it's just "blind" advices in the dark!