0
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();
    }

}   

}

NullPointerException当我line channel = session.openChannel("exec") 将代码放入superclass. 这是因为会话和通道变量没有被继承还是其他原因?

这是我正在建立连接的超类代码。

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();
    }

}

这就是我调用 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;



}
4

1 回答 1

1

看起来该session字段未在其ImageTypeDeployer或其任何超类的构造函数中初始化,因此当您尝试调用session.openChannel. 这解释了在您的makeAConnection方法中初始化它的必要性。

于 2013-09-30T10:26:50.833 回答