函数在Main方法中有效,但在另一个类中不起作用

时间:2015-04-18 07:44:02

标签: java function tweets

我正面临这个奇怪的问题。我编写了以下函数,用于在指定日期之间获取用户的推文:

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

该函数在main方法中工作得非常好,并返回推文集合。但是,当我在另一个类中的另一个函数中添加这行代码时,应用程序不会返回任何内容。相反,它挂起了。这是我的课程,我在其中添加了我的功能:

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;

        }
    }
}

1 个答案:

答案 0 :(得分:0)

NewsCollector应该用作对象吗?它看起来不像那样,所以你可能想让你的方法保持静态。

相关问题