简体   繁体   中英

I am using JSch and connection and session objects not inherited in subclass

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSchException;

public class ImageTypeDeployer extends MainImageDeployer {

public ImageTypeDeployer(String VolID,String oS,String imageName){

    String command="some command"

    try {        
        channel=session.openChannel("exec");
        channel.setInputStream(null);
        ((ChannelExec)channel).setCommand(command);
        channel.connect();
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}   

}

I am getting a NullPointerException on the line channel = session.openChannel("exec") and the same thing works when I put the code in the superclass . Is this because session and channel variables are not inherited or is it something else ?

This is the superclass code where I am making a connection.

public void makeAConnection(){

    inputFile = RESTEngine.getFilePath();
    Properties defaultProps = new Properties();
    try {
        fin = new FileInputStream(inputFile);
        defaultProps.load(fin);
        fin.close();
    }
    catch(FileNotFoundException e1){
        System.out.println("The properties file supposed to contain PowerVC Authorization parameters was not found.");
        e1.printStackTrace();
        System.exit(-1); 
    }
    catch(IOException e1){
        System.out.println("An exception occured while trying to open the properties file");
        e1.printStackTrace();
        System.exit(-1);
    }
    // assign variables from Input file with default value as null
    user = defaultProps.getProperty("UserID", null);
    host = defaultProps.getProperty("PowerVC_IP_ADDRESS", null);
    password = defaultProps.getProperty("UserPass" ,null );

    jsch = new JSch();
    try {
        session = jsch.getSession(user, host, 22);
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties(); 
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel=session.openChannel("exec");
        channel.setInputStream(null);

        try {
            in = channel.getInputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Connection Successful");
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        System.out.println("Unable to connect");
        e.printStackTrace();
    }

}

This is how I am calling the ImageTypeDeployer

protected Boolean doInBackground() throws Exception {

    makeAConnection();
    for(String imageName : volIDMap.keySet()){

        String volID = volIDMap.get(imageName);
        String oS = osMap.get(imageName);
        if (oS.equalsIgnoreCase("aix")){

            MainImageDeployer imageDeployer = new ImageTypeDeployer(volID, oS, imageName);

        }


    }
    return null;



}

It appears the session field is not initialized in the constructor of ImageTypeDeployer or any of its superclasses, so it is null when you try to call session.openChannel . That explains the need to initialize it in your makeAConnection method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM