Reputation: 276
I am trying to execute separate queries in different methods, the code below is not my actual code but is just an example of the error, i have
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
called in both methods, however i get the error
IllegalStateException: Cannot use this builder any longer, build() has already been called
if i make it global then i get the same error, this will probably be just me being stupid but i cant work it out, iv been staring at it for too long now.
here is my code:
ConfigurationBuilder cb = new ConfigurationBuilder();
double lat = 51.5171;
double lon = 0.1062;
double res = 37;
String resUnit = "mi";
public void setup(){
cb.setOAuthConsumerKey("XXXXXXXXXXXXXXXXXXXXXx");
cb.setOAuthConsumerSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
cb.setOAuthAccessToken("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
cb.setOAuthAccessTokenSecret("XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
queryLon();
queryBir();
}
void queryLon(){
try {
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
for (int j = 0; j < 5; j++) {
Query query = new Query();
query.setRpp(100);
QueryResult result = twitter.search(new Query().geoCode(new GeoLocation(lat,lon), res, resUnit));
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
//User u= twitter.showUser("twitter");
String location = t.getLocation();
println("Location: " + location);
}
}
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
}
void queryBir(){
try {
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
for (int j = 0; j < 5; j++) {
Query query = new Query();
query.setRpp(100);
QueryResult result = twitter.search(new Query().geoCode(new GeoLocation(lat,lon), res, resUnit));
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String location = t.getLocation();
println("Location: " + location);
}
}
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
}
any help would be appreciated, cheers.
forgot to mention im using Twitter4j and Processing
Upvotes: 0
Views: 1195
Reputation: 53666
Looks pretty clear: the error tells us that build()
can only be called once, and you're clearly trying to call it multiple times. So, simple solution: when you want to make a new TwitterFactory, either capture the result of cb.build()
in a variable the first time you call it, and then reuse that, or create a new instance of ConfigurationBuilder
and call its build() everty time you create a new TwitterFactory
, although that seems pretty wasteful. Try to capture your cb.build()
first.
Upvotes: 3