-3

In my vb.net I have this code:

Private WithEvents RF As SiritController2._0.RFIDController

Public Sub StartReaders()
    RF = New SiritController2._0.RFIDController(HP.RfIdAddress, HP.RfIdUserName, HP.RfIdPassword, HP.RfIdAntena)
EndSub

In my c# I have this code:

private RFIDController reader;

private void Start()
{
    reader = new RFIDController(HP.RfIdAddress, "HP.RfIdUserName", "HP.RfIdPassword", HP.RfIdAntena);
    reader.DetectRFID += Reader_DetectRFID;
}

These 2 codes are calling the same class on the same constructor

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());
  }
}

Why is it that I get an error of

"Object reference not set to an instance"

in my C# code but not in my VB code? Upon inspection, I can see my parameters get filled.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ibanez1408
  • 4,550
  • 10
  • 59
  • 110
  • 1
    What line is throwing the "Object reference not set to an instance" error? – Jake Steffen May 30 '19 at 19:35
  • My bet: In the C# program, the HP is actually D̶e̶l̶l̶ null... –  May 30 '19 at 19:35
  • @Jake in the class methid that is called – Ibanez1408 May 30 '19 at 19:40
  • @elgonzo no it's not. Even if i use the actual string itself still i get the sane error. – Ibanez1408 May 30 '19 at 19:41
  • Well, follow the link in the first comment. It provides ample explanation about the nature of this particular exception/error, common scenarios causing it as well as suggestions for how to debug and troubleshoot... –  May 30 '19 at 19:45
  • Can you provide an answer to Jake's comment? (What line number is throwing the error? Knowing what line of code is giving the error will help the community to help you.) – DeadZone May 30 '19 at 19:47
  • 1
    Your passing the objects/props as strings themselves, `"HP.RfIdUserName", "HP.RfIdPassword"`, remove the quotes `"`, this *could* be an issue. – Trevor May 30 '19 at 19:58
  • That code looks like the constructor (`Sub New`) of a decompiled VB debug build. There is no need for `RFIDController.__ENCAddToList` method and the static `__ENCList` field that it uses; that is only for the VB debugger use. – TnTinMn May 30 '19 at 20:24

1 Answers1

-1

This

reader = new RFIDController(HP.RfIdAddress, "HP.RfIdUserName", "HP.RfIdPassword", HP.RfIdAntena);

Should be:

reader = new RFIDController(HP.RfIdAddress, HR.RfIdUserName, HP.RfIdPassword, HP.RfIdAntena);

Also not every Exception has an InnerException. So that can cause the NullReferenceException, hiding the real error when the catch block calls:

ex.InnerException.ToString()
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67