title: Unable to download files from FTP server
body: <p>I've created Java function that downloads files from FTP server. It works fine from my local machine. But I need to run it under linux server (means another host and port). And the function gives an error </p>
  
  <pre><code>The collection, array, map, iterator, or enumeration portion of a for statement cannot be null
  </code></pre>
  
  <p>Caused in a line with the code:</p>
  
  <pre><code>for(String f : ftpNames) {
      ftpclient.retrieveFile(f, os); // os is OutputStream
  }
  </code></pre>
  
  <p>So it doesn't see the files...
  I added</p>
  
  <pre><code>ftpclient.enterRemotePassiveMode();
  </code></pre>
  
  <p>And <code>ftpclient.getPassiveHost()</code> returns <code>227 Entering Passive Mode (x,x,x,x,204,15)</code></p>
  
  <p>Tried to list and download them via <strong>shell</strong> - it works.</p>
  
  <p>How should I modify my code to solve the problem? Thanks.</p>
  
  <p><strong>UPD</strong>. I got log from FTP server I'm trying to get files from, and there is such string:</p>
  
  <pre><code> 425 Cannot open data connection
  </code></pre>
  
  <p>Full code:</p>
  
  <pre><code>static boolean ftpFilesDownload(String ip, int port, String login, String passwd, String ftpdir, String localdir) throws IOException {
          Boolean result = false;
  
          FTPClient client = new FTPClient();
          String separator = File.separator;
  
  
          try {                             
              client.connect(ip, port);    
              System.out.println(client.getReplyString());
              client.login(login, passwd);
              System.out.println(client.getReplyString());
  
              client.setControlKeepAliveTimeout(1000*60*5);
              client.setControlKeepAliveReplyTimeout(1000*60*5);
              client.setFileType(FTP.BINARY_FILE_TYPE);
              System.out.println("client setFileType success");    
  
              client.changeWorkingDirectory(ftpdir);
              System.out.println(client.getReplyString());
              client.printWorkingDirectory();
              System.out.println("directory changed");                                 
  
              FTPFile[] ftpFiles = client.listFiles();
              System.out.println(ftpFiles);                          
  
              String[] ftpNames = client.listNames();
              System.out.println("the files are " + Arrays.toString(ftpNames)); // so null here...
                for(String f : ftpNames) {                
              String localfile = localdir + f;
              OutputStream os = new FileOutputStream(localfile);
              try  {
                  result = client.retrieveFile(f, os);
                  System.out.println("DOWNLOADING STARTED);
                  System.out.println(client.getReplyString());
                  client.noop();
              }
              catch(Exception e) {
                  System.out.println(e);                    
                  result = false;
              }
              finally {
                  if(os != null)
                      os.close();
                  }
              }
              client.logout();
              System.out.println(client.getReplyString());
          }
          catch(Exception e)
          {
              System.out.println(e);
              result = false;
          }
          finally
          {
              try
              {
                  client.disconnect();
              }
              catch(Exception e)
              {
                  System.out.println(e);
                  result = false;
              }
          }
          return result;
      }
  </code></pre>