簡體   English   中英

函數在Main方法中起作用,但在另一個類中不起作用

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

我正面臨這個奇怪的問題。 我編寫了以下函數,該函數在指定日期之間獲取用戶的推文:

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

該函數在main方法內部絕對可以正常工作,並返回tweet的集合。 但是,當我將此代碼行添加到其他類中的另一個函數中時,應用程序不返回任何內容。 而是,它掛了。 這是我在其中添加函數的類:

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;

        }
    }
}

是否應該將NewsCollector用作對象? 它看起來不是那樣,所以您可能希望將方法設為靜態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM