0

The documentation on this is fairly bad. Followed guides, still encountering problems.

Relates to OAuth on the Twitter API via the Twitter4J library.

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

The null pointer exception is being thrown on this line;

if (pin.length() > 0) {

And I cannot see why this is happening or what this section of code actually does. I've tried commenting out that whole try/catch section of code surrounding the pin.length() piece of code and this ends up in an endless loop generating new authorisation URLs.

Any ideas or pointers?

Michael Cropper
  • 872
  • 1
  • 10
  • 28
  • `br.readLine();` can return null. Please read the Javadoc on BufferedReader for when that can happen. – OneCricketeer Aug 01 '16 at 21:07
  • Here's a tip: `Scanner sc = new Scanner(System.in); String pin = sc.readLine();` – OneCricketeer Aug 01 '16 at 21:09
  • BufferedReader.readline `Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached`. It is a trivial NPE – Oleg Sklyar Aug 01 '16 at 21:12
  • So what is the solution to this? Tried wrapping the 'pin.length()' section in an 'if (pin != null) {' section, yet this results in an infinite loop again. – Michael Cropper Aug 01 '16 at 21:26

0 Answers0