Exception in thread "main" java.lang.NullPointerException

0

I want to see if they help me see why I get this error, if everything from my point of view is correct, but it points me to a null pointer.  Here is the code that I am testing.

The null marks it in the if

    public static void ListaDoc(String pUser, String pPass,String pHost, int pPort, String remotepath)
        throws Exception {

    JSch sftp = new JSch();
    // Instancio el objeto session para la transferencia
    Session session = null;
    // instancio el canal sftp
    ChannelSftp channelSftp = null;

    try {
        // Inciciamos el JSch con el usuario, host y puerto
        session = sftp.getSession(pUser, pHost, pPort);
        // Seteamos el password
        session.setPassword(pPass);
        // El SFTP requiere un intercambio de claves
        // con esta propiedad le decimos que acepte la clave
        // sin pedir confirmación
        Properties prop = new Properties();
        prop.put("StrictHostKeyChecking", "no");
        session.setConfig(prop);
        session.connect();

        // Abrimos el canal de sftp y conectamos
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();

        channelSftp.ls(remotepath);

    } catch (SftpException e) {
        throw new Exception(e);
    } catch (JSchException e) {
        throw new Exception(e);
    } finally {
        // Cerramos el canal y session
        if (channelSftp.isConnected()) {  // <--- Línea 71
            channelSftp.disconnect();}

        if (session.isConnected()){
            session.disconnect();}
    }// end finally
}// end ListaDoc

public static void main(String[] args) throws Exception{
   Descargar(USERNAME, PASSWORD, HOST, PORT, LOCALPATH, REMOTEPATH,FILE); // <-- Linea 121
} // End main

asked by Carlos Cascante 28.08.2017 в 01:53
source

2 answers

0

Good, seeing your code it seems that the problem does not have to be in the if , it can also be in the lines where you open the channel:

ChannelSftp channelSftp = null; //aquí lo inicializas como null
try {
    // Abrimos el canal de sftp y conectamos
    channelSftp = (ChannelSftp) session.openChannel("sftp"); //aquí
    channelSftp.connect();
    //.....
    }
    //CATCHS
    finally
    {
         // Cerramos el canal y session
         if (channelSftp.isConnected()) {  // <--- Línea 71
         channelSftp.disconnect();}
    }

The finally code will be executed whether or not an exception occurs. If for some reason you could not open the channel with line channelSftp = (ChannelSftp) session.openChannel("sftp"); , your variable channelSftp will be null (since you initialized it that way) so in the finally you can not call to the isConnected() method.

Check that both channelSftp and session are not null .

    
answered by 28.08.2017 в 11:00
0

import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Properties;

/ **  *  * @author ccascante  * / public class Main {

static final String USERNAME = "mobile";
static String HOST = "201.218.222.7";
static int PORT = 55507;
static String PASSWORD = "transferencias2$";
static String REMOTEPATH ="/mobile/java/";/*Agregar el working file para la descarga*/
static String LOCALPATH="C:/Cargar/SFTP/";/*Agregar el working file para la descarga*/
static String FILE="Avellanas.bmp";/*Archivo que deseamos descargar*/


public static void Descargar(String pUser, String pPass,String pHost, int pPort, String localpath, String remotepath, String file)
        throws Exception {
    JSch sftp = new JSch();
    // Instancio el objeto session para la transferencia
    Session session = null;
    // instancio el canal sftp
    ChannelSftp channelSftp = null;
    try {
        // Inciciamos el JSch con el usuario, host y puerto
        session = sftp.getSession(pUser, pHost, pPort);
        // Seteamos el password
        session.setPassword(pPass);
        // El SFTP requiere un intercambio de claves
        // con esta propiedad le decimos que acepte la clave
        // sin pedir confirmación
        Properties prop = new Properties();
        prop.put("StrictHostKeyChecking", "no");
        session.setConfig(prop);
        session.connect();

        // Abrimos el canal de sftp y conectamos
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();
        System.out.println( ""+channelSftp.isConnected());
        // Convertimos el archivo a transferir en un OutputStream donde se va a guardar
        OutputStream os = new BufferedOutputStream(new FileOutputStream(
                localpath+'/'+file));
        // Iniciamos la transferencia
        channelSftp.get(remotepath+'/'+file, os);
    } catch (JSchException e) {
        throw new Exception(e);
    } catch (SftpException e){
        throw new Exception(e);
    } catch (FileNotFoundException e){
        throw new Exception(e);
    }finally{
    // Cerramos el canal y session
        if (channelSftp.isConnected())
            channelSftp.disconnect();
        if (session.isConnected())
            session.disconnect();
    }// end finally
}// end Descargar


    public static void ListaDoc(String pUser, String pPass,String pHost, int pPort, String remotepath)
        throws Exception {
    JSch sftp = new JSch();
    // Instancio el objeto session para la transferencia
    Session session = null;
    // instancio el canal sftp
    ChannelSftp channelSftp = null;

    try {
        // Inciciamos el JSch con el usuario, host y puerto
        session = sftp.getSession(pUser, pHost, pPort);
        // Seteamos el password
        session.setPassword(pPass);
        // El SFTP requiere un intercambio de claves
        // con esta propiedad le decimos que acepte la clave
        // sin pedir confirmación
        Properties prop = new Properties();
        prop.put("StrictHostKeyChecking", "no");
        session.setConfig(prop);
        session.connect();

        // Abrimos el canal de sftp y conectamos
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();

        channelSftp.ls(remotepath);

    } catch (SftpException e) {
        throw new Exception(e);
    } catch (JSchException e) {
        throw new Exception(e);
    } finally {
        // Cerramos el canal y session
        if (channelSftp.isConnected())
            channelSftp.disconnect();
        if (session.isConnected())
            session.disconnect();
    }// end finally
}// end ListaDoc

public static void main(String[] args) throws Exception{
        Descargar(USERNAME, PASSWORD, HOST, PORT, LOCALPATH, REMOTEPATH,FILE);
} // End main

}

    
answered by 29.08.2017 в 02:05