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)

2
  • Did you pass in any parameters?
    – Reimeus
    Dec 7, 2014 at 22:55
  • So presumably you didn't provide any command line arguments, so args has a length of 0... you can't store values in array elements which don't exist.
    – Jon Skeet
    Dec 7, 2014 at 22:55

1 Answer 1

1

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";   
2
  • first way i got : Array constants can only be used in initializers, but the second way works fine , thanks Dec 7, 2014 at 23:00
  • @user4335407 Edited. Sorry. Glad to help. Dec 7, 2014 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.