1

I created program in java. which reads files from several ftp servers. But it reads first file by bufferreader and second time occures NullPointerException. Here is my code:

    while(true){//READ .CSV AND WRITE TO DB
        client=new FTPClient();
        try {
            client.connect(ftps.getIp());
            client.enterLocalActiveMode();
            client.login(ftps.getLogin(),ftps.getPassword());
            System.out.println("Connected with ftp "+ftps.getName());
            String[] files = client.listNames();
            InputStream ins;
            BufferedReader bf;
            for(int i=0;i<files.length;i++){
                ins=client.retrieveFileStream(files[i]);
                bf= new BufferedReader(new InputStreamReader(ins));
                try {
                    String str = "";
                    do
                    {
                        lines.add(str);                            
                        str = bf.readLine();
                    } while (!str.equals(null));
                }catch (NullPointerException ex){                        
                }
                catch (Exception ex){
                    System.out.println("Error while reading file "+files[i]+" from ftp "+ftps.getName()+" ERROR "+ex.toString());
                }
                finally {
                    bf.close();                        
                }
            }
        } catch (Exception e) {
            System.out.println("Error in executing ftp "+ftps.getName()+e.toString());

        }finally {
            if(client.isConnected())
                try{
                    client.disconnect();
                }catch (Exception ex){
                    System.out.println("Error while disconnecting with  ftp "+ftps.getName());
                }
                System.out.println("Disconnected with ftp "+ftps.getName());
        }

Error in executing ftp ftpserver's name appeares in console.

  • Have you checked what the files array consists of? Maybe there's a null at some position? – Michel_T. Feb 02 '17 at 13:53
  • And where is it occuring ? Where is the stacktrace ? – AxelH Feb 02 '17 at 13:54
  • 1
    `!str.equals(null)` I think problem is here. So you're wait while `str` is `null`. When it's `null` you have a NPE. Change it to `(str != null)` – Michel_T. Feb 02 '17 at 13:59
  • But how did he managed to read the first file without a NPE (or is it not talking of that one since it is `catch` (bad design !)) – AxelH Feb 02 '17 at 14:03
  • Did he? He got all data from the file and _before_ BufferedReader should be reinit the app chashed, so he disided that chash happens when BufferedReader reinits. It's just my opinion - he hasn't given any additional information still. – Michel_T. Feb 02 '17 at 14:11
  • 2
    BTW, an empty catch block, as in `catch (NullPointerException ex){}` is _really_ a bad idea. – lukas84 Feb 02 '17 at 14:28
  • client.completePendingCommand – Feruz Sadirov Feb 03 '17 at 12:21

0 Answers0