4

So I am having this code that should read from every line from every text document in an ftp directoy, the thing is that is only works for det first file because when it comes to the second I can see in the log that the second also gets listed but when it wants to read it just chrash, this is the code

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();}

error code:

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

the error is pointing to this line

reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
Emilio Gaines
  • 95
  • 1
  • 10

1 Answers1

1
in = ftpClient.retrieveFileStream(name);

This line is returning you an input stream which is wither null or not instantiated properly. So please check in that perticuler method and always it is better to use null checks, like

if(in != null)

If still you are getting the same exception please provide the above mentioned method code so that we can figure it out.

Manindar
  • 999
  • 2
  • 14
  • 30
  • Im not getting the exception anymore but it is still not reading the second file – Emilio Gaines Jul 14 '15 at 12:30
  • Looks like there is no problem in the code you mentioned above. But i suspect there might be some problem in **retrieveFileStream(name)** method. Please check again into that method or let me know what does the method do so that i can help you. – Manindar Jul 14 '15 at 12:44
  • it retrieve the content of a file without downloading it from a ftp-server sort-of – Emilio Gaines Jul 14 '15 at 12:48
  • While reading from the stream you are downloading the contents of course. How could you see it in your Android app else? You only are not saving the contents to file. – greenapps Jul 14 '15 at 12:56
  • I am having the same issue here, did you managed to fix this? – visionix visionix May 09 '16 at 10:00