Twitter LED NullPointerException
in
Contributed Library Questions
•
2 years ago
Hi Everyone,
This is my first post. I have only been using Processing for about 2-3 days, I assume i have posted it in the right Section.
Basically I'm trying to get my LED Scroller to display the latest posts from Twitter. I'm currently using code from this website that was made by Tom Lynch. Since Tom's post Twitter has, as you probably know I'mplemented OAuth which means Toms script no longer works (just returning a 401 error). So i have tried to put the OAuth requirements on the code and so far i haven't had any problems with that part (although no doubt something will arise). But i do get a NullPointerException error when i try to run the program. I have a feeling it is something to do with OAuth because i didn't get this error previously. Both of the codes are below. If anyone could possibly help me out or point me in the right direction i would be so grateful.
Original Code Used By Tom Lynch:
Code Edited By Me To Include OAuth:
This is my first post. I have only been using Processing for about 2-3 days, I assume i have posted it in the right Section.
Basically I'm trying to get my LED Scroller to display the latest posts from Twitter. I'm currently using code from this website that was made by Tom Lynch. Since Tom's post Twitter has, as you probably know I'mplemented OAuth which means Toms script no longer works (just returning a 401 error). So i have tried to put the OAuth requirements on the code and so far i haven't had any problems with that part (although no doubt something will arise). But i do get a NullPointerException error when i try to run the program. I have a feeling it is something to do with OAuth because i didn't get this error previously. Both of the codes are below. If anyone could possibly help me out or point me in the right direction i would be so grateful.
Original Code Used By Tom Lynch:
- import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.http.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import processing.serial.*;
int message_no = 0;
String message;
int state = 0;
boolean ready = true;
Serial display;
Twitter twitter;
int num_messages;
void setup() {
display = new Serial(this, Serial.list()[0]);
twitter = new Twitter("username", "password");
}
void draw() {
if (state == 0) {
try {
java.util.List statuses = twitter.getPublicTimeline();
Status status = (Status)statuses.get(message_no);
message = status.getText();
num_messages = statuses.size();
}
catch (TwitterException e) {
println(e.getStatusCode());
}
}
updateDisplay();
}
String outputMessage() {
String theseChars = "05" + message + "06";
char check = 0;
for (int c = 0; c < theseChars.length(); c++) {
check = char(check ^ theseChars.charAt(c));
}
return "" + message + hex(check, 2) + "";
}
void updateDisplay() {
if (display.available() > 2) {
String serialData = display.readString();
if (serialData.equals("ACK")) {
state++;
ready = true;
} else {
state = 0;
ready = true;
println(" I broke a bit, hold on a second...");
}
println(" " + serialData);
}
if (ready) {
if (state == 0) {
display.write("05");
ready = false;
} else if (state == 1) {
display.write(outputMessage());
ready = false;
} else if (state == 2) {
display.write("06");
ready = false;
} else if (state == 3) {
println(message_no + ": " + message + " - display updated!");
delay(1325 + (message.length() * 90));
message_no++;
state = 0;
ready = true;
if (message_no >= num_messages) { exit(); }
}
}
}
Code Edited By Me To Include OAuth:
- import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.http.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import processing.serial.*;
int message_no = 0;
String consumer_key = "CONSUMER_KEY";
String consumer_secret = "CONSUMER_SECRET";
String oauth_token = "OAUTH_TOKEN";
String oauth_token_secret = "OAUTH_TOKEN_SECRET";
String message;
int state = 0;
boolean ready = true;
Serial display;
Twitter twitter;
int num_messages;
void setup() {
display = new Serial(this, Serial.list()[0]);
Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance (
consumer_key, consumer_secret,
new AccessToken( oauth_token, oauth_token_secret) );
}
void draw() {
if (state == 0) {
try {
java.util.List statuses = twitter.getUserTimeline();
Status status = (Status)statuses.get(message_no);
message = status.getText();
num_messages = statuses.size();
}
catch (TwitterException e) {
println(e.getStatusCode());
}
}
updateDisplay();
}
String outputMessage() {
String theseChars = "05" + message + "06";
char check = 0;
for (int c = 0; c < theseChars.length(); c++) {
check = char(check ^ theseChars.charAt(c));
}
return "" + message + hex(check, 2) + "";
}
void updateDisplay() {
if (display.available() > 2) {
String serialData = display.readString();
if (serialData.equals("ACK")) {
state++;
ready = true;
} else {
state = 0;
ready = true;
println(" I broke a bit, hold on a second...");
}
println(" " + serialData);
}
if (ready) {
if (state == 0) {
display.write("05");
ready = false;
} else if (state == 1) {
display.write(outputMessage());
ready = false;
} else if (state == 2) {
display.write("06");
ready = false;
} else if (state == 3) {
println(message_no + ": " + message + " - display updated!");
delay(1325 + (message.length() * 90));
message_no++;
state = 0;
ready = true;
if (message_no >= num_messages) { exit(); }
}
}
}
1