在我的vb.net中,我有这样的代码:
Private WithEvents RF As SiritController2._0.RFIDController
Public Sub StartReaders()
RF = New SiritController2._0.RFIDController(HP.RfIdAddress, HP.RfIdUserName, HP.RfIdPassword, HP.RfIdAntena)
EndSub
在我的c#中,我有这样的代码:
private RFIDController reader;
private void Start()
{
reader = new RFIDController(HP.RfIdAddress, "HP.RfIdUserName", "HP.RfIdPassword", HP.RfIdAntena);
reader.DetectRFID += Reader_DetectRFID;
}
这两个代码在同一构造函数上调用同一个类
public RFIDController(string HostIP, string userName, string userPass, string AntennaNoPort = "1")
{
RFIDController.__ENCAddToList((object) this);
this.SvcTimerInterval = 45000;
this.ReaderTimeOut = 1000;
this.RaiseEventDelay = 0;
this._lastReadRFTagNumber = string.Empty;
this._LRRFT = new RFIDController.LastReadRFTagNumber();
try
{
if (string.IsNullOrEmpty(userName) | string.IsNullOrEmpty(userPass))
throw new Exception("Reader username or password cannot be empty.");
this.IPAddress = HostIP;
this.ReaderUser = userName;
this.ReaderPW = userPass;
this.AntennaNo = AntennaNoPort;
this.Connect();
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
throw new Exception("Error occured while instantiating controller : " + ex.InnerException.ToString());
}
}
为什么我得到的错误是
“对象引用未设置为实例”
在我的C#代码中而不是VB代码中?经过检查,我可以看到我的参数被填满了。
发布于 2019-05-31 03:56:55
这
reader = new RFIDController(HP.RfIdAddress, "HP.RfIdUserName", "HP.RfIdPassword", HP.RfIdAntena);
应该是:
reader = new RFIDController(HP.RfIdAddress, HR.RfIdUserName, HP.RfIdPassword, HP.RfIdAntena);
而且,并不是每个异常都有InnerException。因此,这会导致NullReferenceException,在catch块调用时隐藏真正的错误:
ex.InnerException.ToString()
https://stackoverflow.com/questions/56384552
复制相似问题