java - What is dereferencing possible null pointer? -
i making program sftp
in netbeans
.
some part of 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); } { 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 resolve these warnings??? can disconnect session
, channel
???
sessiontarget = jsch.getsession(backupuser, backuphost, backupport);
here in line, getsession()
method can throw exception, , hence variables sessiontarget
, channeltarget
null, , in block, accessing variables, may cause null pointer exception.
to avoid this, in block check null before accessing variable.
finally { if (channeltarget != null) { channeltarget.exit(); channeltarget.disconnect(); } if (sessiontarget != null ) { sessiontarget.disconnect(); } }
Comments
Post a Comment