【问题标题】:Function works inside Main method but doesn't work in another class函数在 Main 方法中有效,但在另一个类中无效
【发布时间】:2015-04-18 07:44:02
【问题描述】:

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

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;

        }
    }
}

【问题讨论】:

  • 发布调用你的类的代码
  • 你应该删除口袋妖怪try {} catch,再试一次并发布堆栈跟踪
  • 你得到什么错误。发布堆栈跟踪。

标签: java function tweets


【解决方案1】:

NewsCollector 是否应该用作对象?它看起来不像那样,因此您可能希望将您的方法设为静态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2019-09-27
    相关资源
    最近更新 更多