我有一段代码,应该从FTP目录中的每个文本文档中读取每一行,但是只能对第一个文件起作用,因为当它处理第二个文件时,日志显示第二个文件也被列出,但是在尝试读取时就会崩溃。以下是代码:
public void getComments(){
final FTPClient ftpClient = new FTPClient();
BufferedReader reader = null;
new Thread(new Runnable() {
public void run() {
try {
ftpClient.connect(InetAddress.getByName("31.220.17.2"));
ftpClient.login("mopedsho", "neppi1");
int imageNr = sharedPreferences.getInt("ImageNrCross", 1);
ftpClient.makeDirectory("/public_ftp/Comments/Cross/" + imageNr);
ftpClient.changeWorkingDirectory("/public_ftp/Comments/Cross/" + imageNr);
String[] names = ftpClient.listNames();
if (names == null) {
} else {
BufferedReader reader = null;
for (String name : names) {
if (!name.equals(".") && !name.equals("..")) {
InputStream in = null;
try {
Log.e("File namer", name + "");
in = ftpClient.retrieveFileStream(name);
if (in != null) {
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String firstLine;
while ((firstLine = reader.readLine()) != null) {
final String line = firstLine;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
adapter.add("" + line);
}
});
}
}
in.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
ftpClient.logout();
ftpClient.disconnect();
}
}} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}}
}).start();}
错误代码:
07-14 13:50:27.739 20152-20298/com.emiliogaines.mopedshowcase E/File namer﹕ EmilioGaines4663767201961631433982.txt
07-14 13:50:28.166 20152-20298/com.emiliogaines.mopedshowcase E/File namer﹕ EmilioGaines4663767201961631535180.txt
07-14 13:50:28.306 20152-20298/com.emiliogaines.mopedshowcase E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-6657
Process: com.emiliogaines.mopedshowcase, PID: 20152
java.lang.NullPointerException: lock == null
at java.io.Reader.<init>(Reader.java:64)
at java.io.InputStreamReader.<init>(InputStreamReader.java:77)
at com.emiliogaiines.mopedshowcase.commentLayout$2.run(commentLayout.java:140)
at java.lang.Thread.run(Thread.java:818)
07-14 13:50:28.431 20152-20152/com.emiliogaines.mopedshowcase I/Ti
错误指向这一行。
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));