pin.length()上的Java Twitter4J空指针异常

时间:2016-08-01 21:03:20

标签: java twitter-oauth twitter4j

关于此的文档非常糟糕。跟着导游,仍然遇到问题。

通过Twitter4J库与Twitter API上的OAuth相关联。

ConfigurationBuilder builder = new ConfigurationBuilder();
            builder.setOAuthConsumerKey("");
            builder.setOAuthConsumerSecret("");
            builder.setOAuthAccessToken(null);
            builder.setOAuthAccessTokenSecret(null);
            Configuration configuration = builder.build();
            TwitterFactory factory = new TwitterFactory(configuration);
            Twitter twitter = factory.getInstance();    

RequestToken requestToken;
            try {
                String callbackURL = "/callback";

                requestToken = twitter.getOAuthRequestToken(callbackURL);
                AccessToken accessToken = null;
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                while (null == accessToken) {
                    System.out.println("Open the following URL and grant access to your account:");
                    String requestTokenAuthorisationURL = requestToken.getAuthorizationURL();
                    System.out.println(requestTokenAuthorisationURL);

                    System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
                    String pin = br.readLine();
                    try {
                        if (pin.length() > 0) {
                            accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                        } else {
                            accessToken = twitter.getOAuthAccessToken();
                        }
                    } catch (TwitterException te) {
                        if (401 == te.getStatusCode()) {
                            System.out.println("Unable to get the access token.");
                        } else {
                            te.printStackTrace();
                        }
                    }
                }
            } catch (TwitterException ex) {
                Logger.getLogger(EmployerAdvertisingTwitter.class.getName()).log(Level.SEVERE, null, ex);
            }

此行上抛出空指针异常;

if (pin.length() > 0) {

我无法理解为什么会发生这种情况或这部分代码实际上做了什么。我已经尝试评论围绕pin.length()代码片段的整个try / catch部分代码,这最终会在无限循环中生成新的授权URL。

任何想法或指示?

0 个答案:

没有答案