FTP download stops at file #146
<p>I wrote this class (from examples) to download the header of all the files contained in a remote FTP folder. It works well, but when it approaches to download the file #146 it stops with a NullPointException. The file #146 exists and I can download it as a single file actually.</p>

<p>In the method remotePathLong contains all the remote folders written in a single line and spaced with the space character.</p>

<pre><code>public void downloadHeader(String remotePathLong, String destPath, int bytes) {

    String remotePath;
    FTPFile[] fileList;
    String[] fileNameList;

    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(server);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        int indice = 0;
        int iP = 1;
        File downloadFile;
        String destFile;
        String remoteFile;
        byte[] bytesArray;
        int bytesRead = -1;
        while ((remotePath = getPath(remotePathLong, iP)) != null) {
            System.out.println("Loading file list from the server.....");
            fileNameList = ftpClient.listNames(remotePath);
            for (String file : fileNameList) {
                indice += 1;
                System.out.println(indice + " - Downloading: " + file);

                //Select files
                destFile = destPath.concat(file);

                downloadFile = new File(destFile);
                outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));

                //Download remote file (from ftp)
                remoteFile = remotePath.concat(file);

                inputStream = ftpClient.retrieveFileStream(remoteFile);
                bytesArray = new byte[bytes];
                bytesRead = inputStream.read(bytesArray);
                outputStream.write(bytesArray);

                //Save into file

                outputStream.close();
                inputStream.close();
                iP += 1;
            }
        }

    } catch (IOException ex) {
    } final{
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        }
        catch (IOException ex1) {
            System.out.println("Error: " + ex1.getMessage());
        }
}
</code></pre>

<p>when it reaches bytesRead = inputStream.read(bytesArray), at the iteration #146 it gives the error. But if at the same iteration i reinitialize the connection it works.
Does anybody have a suggestion please?</p>
