Java 什么是取消引用可能的空指针?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21596262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:34:01  来源:igfitidea点击:

What is dereferencing possible null pointer?

javanetbeansnullpointerexceptiondereference

提问by Ankit Lamba

I am making a program for SFTPin NetBeans.

我正在为SFTPin制作一个程序NetBeans

Some part of My code:

我的代码的某些部分:

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
        }

I'm getting warning dereferencing possible null pointer, how can I resolve these warnings??? Where I can disconnect my Sessionand Channel???

我收到警告dereferencing possible null pointer,我该如何解决这些警告???我在哪里可以断开我的SessionChannel???

采纳答案by Abimaran Kugathasan

sessionTarget = jsch.getSession(backupUser, backupHost, backupPort);Here in this line, getSession()method can throw an Exception, and hence the variables sessionTargetand channelTargetwill be null, and in the finally block, you are accessing those variables, which may cause null pointer exception.

sessionTarget = jsch.getSession(backupUser, backupHost, backupPort);在这一行中,getSession()方法可以抛出异常,因此变量sessionTargetchannelTarget将为空,并且在 finally 块中,您正在访问这些变量,这可能会导致空指针异常。

To avoid this, in the finally block check for null before accessing the variable.

为了避免这种情况,在访问变量之前,在 finally 块中检查 null。

finally {
  if (channelTarget != null) {
       channelTarget.exit();     
       channelTarget.disconnect();  
  }
  if (sessionTarget != null ) {
       sessionTarget.disconnect();  
  }
}

回答by sadhu

That means: what if your channelTargetand sessionTargetare null in your finally block? Check them for null to avoid the warning.

这意味着:如果你的channelTargetsessionTarget在你的 finally 块中为空怎么办?检查它们是否为空以避免警告。

回答by ErrorNotFoundException

I think netbeans is just trying to warn you that these may be null which may not be necasarilly true. You can choose to disable the warnings though. Just place your cursor on the warning press ALT+ENTERand choose from the choices even to disable the warnings.

我认为 netbeans 只是想警告您这些可能为 null,这可能并非完全正确。不过,您可以选择禁用警告。只需将光标放在警告按钮上,然后ALT+ENTER从选项中进行选择,甚至禁用警告。