Java错误:java.lang.ArrayIndexOutOfBoundsException:0

时间:2014-12-07 22:53:38

标签: java search twitter4j

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);
    }

我收到此错误我不知道为什么..

线程中的异常&#34; main&#34; java.lang.ArrayIndexOutOfBoundsException:0     在twitter4j.examples.search.SearchTweets.main(SearchTweets.java:34)

1 个答案:

答案 0 :(得分:1)

因为在程序的执行中设置了String[] args,并且您没有传递任何命令行参数。由于您似乎想要用3个编译时常量替换它们,您可以初始化args,如

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