我正在为 SFTP
在 NetBeans
制作一个程序。
我的代码的一部分:
com.jcraft.jsch.Session sessionTarget = null;
com.jcraft.jsch.ChannelSftp channelTarget = null;
try {
sessionTarget = jsch.getSession(backupUser, backupHost, backupPort);
sessionTarget.setPassword(backupPassword);
sessionTarget.setConfig("StrictHostKeyChecking", "no");
sessionTarget.connect();
channelTarget = (ChannelSftp) sessionTarget.openChannel("sftp");
channelTarget.connect();
System.out.println("Target Channel Connected");
} catch (JSchException e) {
System.out.println("Error Occured ======== Connection not estabilished");
log.error("Error Occured ======== Connection not estabilished", e);
} finally {
channelTarget.exit(); // Warning : dereferencing possible null pointer
channelTarget.disconnect(); // Warning : dereferencing possible null pointer
sessionTarget.disconnect(); // Warning : dereferencing possible null pointer
}
我收到警告 dereferencing possible null pointer
,我该如何解决这些警告???我在哪里可以断开我的 Session
和 Channel
???
原文由 earthmover 发布,翻译遵循 CC BY-SA 4.0 许可协议
sessionTarget = jsch.getSession(backupUser, backupHost, backupPort);
Here in this line,getSession()
method can throw an Exception, and hence the variablessessionTarget
andchannelTarget
will be null, and in在 finally 块中,您正在访问那些可能导致空指针异常的变量。为避免这种情况,在
finally
块中,在访问变量之前检查是否为 null。