Search code examples
javasearchtwitter4j

Java error : java.lang.ArrayIndexOutOfBoundsException: 0


public static void main(String[] args) {
    args[0] = "derp";
    args[1] = "herp";
    args[2] = "lerp";       

    if (args.length < 1) {
        System.out.println("what?");
        System.exit(-1);
    }Twitter twitter = new TwitterFactory().getInstance();
    try {
        Query query = new Query(args[0]);
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
            }
        } while ((query = result.nextQuery()) != null);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
        System.exit(-1);
    }

i am getting this error i dont know why..

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at twitter4j.examples.search.SearchTweets.main(SearchTweets.java:34)


Solution

  • Because the String[] args is set on your program's execution, and you aren't passing any command line arguments. Since it appears you want to replace them with 3 compile time constants you could initialize args like

    args = new String[3];
    args[0] = "derp";
    args[1] = "herp";
    args[2] = "lerp";