0

To be clear, to all of the guys who rush and say that these type of posts are duplicate without even reading it: this is not a type of question in which i ask what null is and how can i manage these exceptions, here i ask why twitter's API returns to my method A null object seemingly random.

I am creating a java application that interacts with Twitter API using the library Twitter4J. I want to download a big amount of tweets, and then do the statistics on the offline data. Tweets are saved in a NoSQL database (elasticsearch).

My code was doing fine when it started printing the tweets only on the console for testing. When my program hit the limit of max tweets it slept until the reset of twitter limitation (more than 1.000.000 was printed and got zero errors), the problem came up after i started saving the tweets in my database, after some loops, i get a java.lang.NullPointerException in this exact statement if (searchTweetsRateLimit.getRemaining() == 0). Any suggestions?

 public static void main(String[] args) throws TwitterException {
    int totalTweets = 0;
    long maxID = -1;        

    twitter4j.Twitter twitter = getTwitter();

    RestClient restclient = RestClient.builder(
            new HttpHost("localhost",9200,"http"),
            new HttpHost("localhost",9201,"http")).build();


        Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search");

        //  This finds the rate limit specifically for doing the search API call we use in this program
        RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets");



        System.out.printf("You have %d calls remaining out of %d, Limit resets in %d seconds\n",
                          searchTweetsRateLimit.getRemaining(),
                          searchTweetsRateLimit.getLimit(),
                          searchTweetsRateLimit.getSecondsUntilReset());

        int i = 10;


        //  This is the loop that retrieve multiple blocks of tweets from Twitter
        for (int queryNumber=0;queryNumber < MAX_QUERIES; queryNumber++)
        {
            System.out.printf("\n\n!!! Starting loop %d\n\n", queryNumber);

            //  Do we need to delay because we've already hit our rate limits?
            if (searchTweetsRateLimit.getRemaining() == 0)
            {
                //  Yes we do, unfortunately ...
                System.out.printf("!!! Sleeping for %d seconds due to rate limits\n", searchTweetsRateLimit.getSecondsUntilReset());

                //  If you sleep exactly the number of seconds, you can make your query a bit too early
                //  and still get an error for exceeding rate limitations

                Thread.sleep((searchTweetsRateLimit.getSecondsUntilReset()+2) * 1000l);
            }

            Query q = new Query(SEARCH_TERM);           // Search for tweets that contains this term
            q.setCount(TWEETS_PER_QUERY);               // How many tweets, max, to retrieve
            q.resultType(null);                     // Get all tweets
            q.setLang("en");                            // English language tweets, please
Chris Ftw
  • 1
  • 5
  • According to its javadoc, `RateLimitStatus#getRemaining` returns an `int`, so the only possibility for a NPE would be that `searchTweetsRateLimit` is `null`. Buf it that were so, the NPE would occur in your `System.out.printf`. Are you sure? – Marvin Jun 08 '17 at 20:04
  • Yes i'm sure I didn't change a thing in my code, the difference now is that i put the data in my database and not just printing it, in previous tests i could see that the `searchTweetsRateLimit.getRemaining()` `searchTweetsRateLimit.getLimit()` `searchTweetsRateLimit.getSecondsUntilReset()` were printed correctly, and now the same code gives me NPE. @Marvin – Chris Ftw Jun 08 '17 at 20:50
  • You can see the printf() output here [link](http://imgur.com/a/As8nu) it returns the integers correctly in the first 800~900 loops, and after that i get NPE – Chris Ftw Jun 08 '17 at 20:56
  • Is there something later in the for loop that could set `searchTweetsRateLimit` to null? – Sean Van Gorder Jun 08 '17 at 21:10
  • Actully threre is: `QueryResult r = twitter.search(q); if (r.getTweets().size() == 0) { break; } // loop through all the tweets and process them. for (Status s: r.getTweets()) { // Increment our count of tweets retrieved totalTweets++; if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } //saving the tweets here i++; } searchTweetsRateLimit = r.getRateLimitStatus(); }` @SeanVanGorder – Chris Ftw Jun 08 '17 at 21:41

0 Answers0