0

This question is related to the following answer posted by Zon.

Transfer folder and subfolders using channelsftp in JSch?

I used the following code to copy a folder from remote to local.

public void downloadDir(String sourcePath, String destPath) throws SftpException { // With subfolders and all files.
    // Create local folders if absent.
    try {
        new File(destPath).mkdirs();
    } catch (Exception e) {
        System.out.println("Error at : " + destPath);
    }
    sftpChannel.lcd(destPath);

    // Copy remote folders one by one.
    lsFolderCopy(sourcePath, destPath); // Separated because loops itself inside for subfolders.
}

private void lsFolderCopy(String sourcePath, String destPath) throws SftpException { // List source (remote, sftp) directory and create a local copy of it - method for every single directory.
    Vector<ChannelSftp.LsEntry> list = sftpChannel.ls(sourcePath); // List source directory structure.
    for (ChannelSftp.LsEntry oListItem : list) { // Iterate objects in the list to get file/folder names.
        if (!oListItem.getAttrs().isDir()) { // If it is a file (not a directory).
            if (!(new File(destPath + "/" + oListItem.getFilename())).exists() || (oListItem.getAttrs().getMTime() > Long.valueOf(new File(destPath + "/" + oListItem.getFilename()).lastModified() / (long) 1000).intValue())) { // Download only if changed later.
                new File(destPath + "/" + oListItem.getFilename());
                sftpChannel.get(sourcePath + "/" + oListItem.getFilename(), destPath + "/" + oListItem.getFilename()); // Grab file from source ([source filename], [destination filename]).
            }
        } else if (!".".equals(oListItem.getFilename() || "..".equals(oListItem.getFilename())) {
            new File(destPath + "/" + oListItem.getFilename()).mkdirs(); // Empty folder copy.
            lsFolderCopy(sourcePath + "/" + oListItem.getFilename(), destPath + "/" + oListItem.getFilename()); // Enter found folder on server to read its contents and create locally.
        }
    }
}

But I am getting a null pointer exception at the following line which I am unable to resolve.

Vector<ChannelSftp.LsEntry> list = sftpChannel.ls(sourcePath); // List source directory structure

I get that it has problem reading the source path but why? Please suggest.

Edited:

This is the stacktrace:

Exception in thread "main" 4: 
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1720)
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1526)
    at org.gradle.CopyTest.downloadDir(CopyTest.java:68)
    at org.gradle.CopyTest.main(CopyTest.java:97)
Caused by: java.lang.NullPointerException
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1543)
    ... 3 more

This exception is getting thrown:

         throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);

From this statement I believe:

((MyPipedInputStream)io_in).updateReadSide();
Community
  • 1
  • 1
k19
  • 83
  • 3
  • 10
  • Because `sftpChannel` is null? – shmosel Jun 15 '16 at 23:17
  • Yes, but why, it should be reading the source directory which is not empty.. – k19 Jun 15 '16 at 23:24
  • And also sftpChannel was able to execute the command before this line..`sftpChannel.lcd(destPath)` – k19 Jun 15 '16 at 23:39
  • Please explain why is the sftpChannel is null... – k19 Jun 15 '16 at 23:48
  • I tried printing the values and both sftpChannel and sourcePath are not null and are printing. – k19 Jun 16 '16 at 00:30
  • Then you're probably pointing to the wrong line. Post your stack trace. – shmosel Jun 16 '16 at 00:32
  • Probably a duplicate of http://stackoverflow.com/questions/37799006/jsch-get-fails-with-nullpointerexception. If you don't think so, please edit your question to include your code for creating the SFTP channel. – Kenster Jun 16 '16 at 14:20
  • The code actually worked fine on other machine, the error was due to sftp not enabled on this particular machine. – k19 Jul 12 '16 at 23:41

0 Answers0