2014-12-07 4 views
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); 
    } 

내가 그나마이 오류가 알고 받고있는 이유 .. 스레드 "주요"java.lang.ArrayIndexOutOfBoundsException의자바 오류 : java.lang.ArrayIndexOutOfBoundsException : 0

예외 : twitter4j.examples에서 0 . search.SearchTweets.main (SearchTweets.java:34)

+0

어떤 매개 변수를 전달 했습니까? – Reimeus

+0

아마 명령 행 인자를 제공하지 않았을 것입니다. 그래서'args'는 길이가 0입니다 ... 존재하지 않는 배열 요소에 값을 저장할 수 없습니다. –

답변

1

String[] args가 프로그램 실행시 설정되어 있고 명령 줄 인수를 전달하지 않기 때문에. 당신이 초기화 할 수있는 3 개의 컴파일 타임 상수로 그들을 대체하기를 원하므로

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

첫 번째 방법은 내가 가지고 : 배열 상수는 이니셜 라이저에서만 사용할 수 있지만 두 번째 방법은 잘 작동합니다. 감사합니다 – user4335407

+0

@ user4335407 편집 됨. 죄송합니다. 기꺼이 도와주세요. –

관련 문제