Search code examples
javatwitter4j

Twitter4j NumberFormatException


I'm using some of Twitter4j examples in their Github repo, but whenever I'm trying to use any username, for instance to get the last tweet, using:

Twitter twitter = new TwitterFactory().getInstance();
Status status = twitter.showStatus(Long.parseLong("userName"));

it gives me the following error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "userName"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:589)
    at java.lang.Long.parseLong(Long.java:631)

Apparently there is other ways to do the same task, but I wonder if anyone has a solution for this problem. Also I'm using 'org.twitter4j:twitter4j-core:4.0.2'.


Solution

  • As Joey pointed out Long.parseLong("userName") is the problem.
    If you use twitter.showStatus(an id) you need the id of the tweet, not the user name. If you want the last tweet by an user you need to do this:

    Twitter twitter = new TwitterFactory().getInstance();
    User user = twitter.showUser(username); //username could be an String or a Long with the id of the user
    Status status = user.getStatus();
    

    And that's it