Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is dereferencing possible null pointer?

I am making a program for SFTP in 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 Session and Channel???

like image 756
earthmover Avatar asked Feb 06 '14 07:02

earthmover


People also ask

What is dereferencing null pointer?

Dereferencing a null pointer always results in undefined behavior and can cause crashes. If the compiler finds a pointer dereference, it treats that pointer as nonnull. As a result, the optimizer may remove null equality checks for dereferenced pointers.

Why can't we dereference null pointer?

Null dereferencingBecause a null pointer does not point to a meaningful object, an attempt to dereference (i.e., access the data stored at that memory location) a null pointer usually (but not always) causes a run-time error or immediate program crash. In C, dereferencing a null pointer is undefined behavior.

Can we dereference a null pointer in C?

Dereferencing a null pointer is undefined behavior. On many platforms, dereferencing a null pointer results in abnormal program termination, but this is not required by the standard.

What is dereferencing a pointer?

Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.


1 Answers

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

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

finally {
  if (channelTarget != null) {
       channelTarget.exit();     
       channelTarget.disconnect();  
  }
  if (sessionTarget != null ) {
       sessionTarget.disconnect();  
  }
}
like image 118
Abimaran Kugathasan Avatar answered Sep 23 '22 20:09

Abimaran Kugathasan