当我在 for 循环的第二次迭代中执行此代码时,InputStreamReader 会引发如下 NPE。该代码在第一次迭代中完美运行,并在第二次迭代中返回以下 NPE。我正在使用代码片段从 FTP 位置读取特定文件的内容并显示它们。请注意所有行,直到新的 InputStreamReader 即使在第二次迭代时也能完美工作。任何想法为什么?
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:61)
at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
at com.test.txtweb.server.task.CallBackRetryTask.main(CallBackRetryTask.java:229)
这是源代码:
public static void main(String[] args){
String strDate = "20130805";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(host);
String pathToFiles = "/path/to/File";
String ftpFileName = "";
List<String> ftpFileNames = null;
InputStream iStream;
if(ftpClient.login(username, password)){
ftpClient.enterLocalPassiveMode();
FTPFile[] ftpFiles = ftpClient.listFiles();
ftpFileNames = new ArrayList<String>();
for (FTPFile ftpFile : ftpFiles) {
ftpFileName = ftpFile.getName();
if(ftpFileName.contains(strDate)){
iStream = ftpClient.retrieveFileStream(pathToFiles + ftpFileName);
System.out.println(ftpClient.getReplyString());
InputStreamReader isr = new InputStreamReader(iStream); //Error on this line on second iteration
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
}