I'm currently playing around with Java, JForms, and the Twitter4j library. Trying to create a way to populate a vector with tweetID's in order to perform actions on them (Retweets, Favorites, replies, etc.). I'm having a bit of a problem with either:
A. Not being able to add to the vector, or
B. Not being able to read from it.
Based on what I've seen from the logger output, I'm positive I'm not adding to the array correctly. Could somebody take a look with me and see what's going on?
main.java (The code to add to the vector with the tweet ID.)
private void btn_refreshTimelineActionPerformed(java.awt.event.ActionEvent evt) {
TwitterUtilities tu = new TwitterUtilities();
Twitter twitter = tu.getConnect();
// Gets tweet information.
List<Status> statuses = null;
try {
statuses = twitter.getHomeTimeline();
} catch (TwitterException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
// Init table.
DefaultTableModel model = (DefaultTableModel) tbl_tweets.getModel();
// Clear table.
model.setRowCount(0);
// Populate table.
for (Status status : statuses)
{
tu.setTargetID(status.getId());
Vector row = new Vector();
row.add(status.getUser().getName());
row.add(status.getText());
model.addRow(row);
}
}
main.java (The part where getTargetTweetID is invoked.)
private void btn_favoriteActionPerformed(java.awt.event.ActionEvent evt) {
TwitterUtilities tu = new TwitterUtilities();
Twitter twitter = tu.getConnect();
int targetIndex = tbl_tweets.getSelectedRow();
long tweetID = tu.getTargetTweetID(targetIndex);
try {
twitter.createFavorite(tweetID);
} catch (TwitterException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
}
TwitterUtilities.java
public class TwitterUtilities {
public Vector tweetIDVector;
// Other stuff... but here's the important stuff.
public void setTargetID(long targetStatus)
{
this.tweetIDVector.addElement(targetStatus);
}
public long getTargetTweetID(int targetIndex)
{
// Get tweet ID for the selected tweet.
Object targetTweetID = this.tweetIDVector.get(targetIndex);
return (long) targetTweetID;
}
}
The error message that I'm getting upon runtime:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
Any help would be greatly appreciated!
EDIT: After working with the suggestions, I've now updated the following code to this new setup:
TwitterUtilities.java
public class TwitterUtilities {
public Vector tweetIDVector = new Vector<>();
// More fun stuff...
public void setTargetID(long targetStatus)
{
this.tweetIDVector.addElement(targetStatus);
for (int i = 0; i <= tweetIDVector.size(); i++)
{
System.out.println(tweetIDVector.get(i));
}
}
And so now the following happens on run:
838641565769871360
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
EDIT 2: Changed the Vector into a List. Loading the Tweet ID's is working. Also changed the <= in the for loop to <. Now it is loading everything the way it's supposed to. Still having issues with getting data from the List in the getTargetTweetID.