Twitter Feed Drawing Image, Error
in
Programming Questions
•
6 months ago
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");
}
1