0

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.

Codename X
  • 31
  • 6
  • 1
    *Based on what I've seen from the logs, I'm positive I'm not adding to the array correctly.* What are you seeing from the logs? Why are you hiding information? – shmosel Mar 06 '17 at 05:40
  • Is it possible that you didn't initialize `tweetIDVector` ? – Nir Alfasi Mar 06 '17 at 05:41
  • Tried to initialize it as an empty vector with `Vector tweetIDVector = new Vector;` and `Vector tweetIDVector = new Vector<>;`. Even tried an ArrayList. Still nothing. – Codename X Mar 06 '17 at 06:01
  • Changed question in order to better explain where I'm reading the error from. – Codename X Mar 06 '17 at 06:06
  • It's still not clear: which line triggers the error ? is it `Object targetTweetID = this.tweetIDVector.get(targetIndex);` ? before the line line that triggers the error you should try to print the content of the vector and the index you're trying to access. – Nir Alfasi Mar 06 '17 at 06:25
  • Ah! My apologies. Been a long day. After taking a look at it, the issue is now starting at the `setTargetID` block, with this error: `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException`. Tried to also initialize it as was suggested earlier, but still didn't work. – Codename X Mar 06 '17 at 06:33
  • 1
    BTW: http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Kevin Krumwiede Mar 06 '17 at 06:52
  • Awesome. I'll start looking into this and seeing if using something better resolves the issue. Will report back. Thanks. – Codename X Mar 06 '17 at 06:54

0 Answers0