c# - Getting exception while reading Connected property on TcpClient instance -


public bool isconnected {     { return _tcpclient == null ? false : _tcpclient.connected; } } 

throws

"object reference not set instance of object."

at

at system.net.sockets.tcpclient.get_connected() @ project.viewmodel.modbusoutputcounter.get_isconnected() in c:...\modbusoutputcounter.cs:line 115

how possible, , how can prevent receiving exception?

edit:

as per svk's anwer problem in disposing, internally called in close() method. workaround:

return _tcpclient?.client != null ? _tcpclient.connected : false; 

according reference source tcpclient, connected directly returns connected of underlying socket. means connected throw nullreferenceexception when socket null. though skimming reference source, found 2 cases when can happen:

  1. when tcpclient has been disposed.
  2. when explicitly set client socket null.

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -