简体   繁体   中英

Function works inside Main method but doesn't work in another class

I am facing this weird issue. I have written following function which fetches tweets of a user between specified dates:

List<Tweet> tweetlist =  TweetManager.getTweets("arynewsofficial", fromdate, todate, null);

The function works absolutely fine inside main method and returns collection of tweets. However, when I add this line of code in another function inside some other class, application doesnt return anything. Rather, it hangs. Here is my class in which I have added my function:

public class NewsCollector {        
    public List<String> collectTweets(String stockdate[], int noofpastimpacts) 
           throws IOException
    {
        try
        {
            int year= Integer.parseInt(stockdate[0]);
            int month= Integer.parseInt(stockdate[1]);
            int day= Integer.parseInt(stockdate[2]);
            List<String> tweetstext = new ArrayList<String>();

            LocalDate currentdate = LocalDate.of(year, month, day);
            LocalDate datefromdate = currentdate.minusDays(noofpastimpacts+1);

            String fromdate= datefromdate.getYear()+ "-" + 
                             datefromdate.getMonthValue() + "-" + 
                             datefromdate.getDayOfMonth();
            LocalDate datetodate = datefromdate.plusDays(noofpastimpacts);

            String todate = datetodate.getYear() + "-" + 
                            datetodate.getMonthValue() + "-" +
                            datetodate.getDayOfMonth();

            List<Tweet> tweetlist =  
                    TweetManager.getTweets("arynewsofficial", fromdate, todate, null);

            List<String[]> data = new ArrayList<String[]>();
            data.add(new String[] {"Text", "Date","Sentiment"});

            for(Tweet tweety: tweetlist)
            {
                data.add(new String[] { tweety.getText(), 
                                tweety.getDate().toString(),
                                "0"});
                tweetstext.add(tweety.getText());
            }

            String csv = "D:/Tweets/"+fromdate+".csv";
            CSVWriter writer = new CSVWriter(new FileWriter(csv));

            writer.writeAll(data);
            writer.close();

            System.out.print("Total Tweets Retreived: "+tweetlist.size() );
            return tweetstext;
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            return null;

        }
    }
}

Is NewsCollector supposed to be used as an object? It doesn't look like that, so you may want to make your method static.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM