So I for the life of me can't figure out how to find this NullReferenceError. I wrote a library for a program called ScannerHelper. It's sole purpose is to turn on and off a barcode reader. The program that it runs on I have no access to the source code, but I talk to the programmer very frequently and this has us both stumped. Here is the trace that we have setup.
Trace
OpenBarPort: Try Opening Barcode port
ScannerHelper Information: 0 : (0):Open(COM6)+
ScannerHelper Information: 0 : (0):IsValidNewlandScanner(COM6)+
ScannerHelper Information: 0 : (0):HasValidName(COM6)+
ScannerHelper Information: 0 : (0):HasValidName(True)-
ScannerHelper Information: 0 : (0):CheckForNewlandScanner(COM6)+
ScannerHelper Information: 0 : (0):WriteCommand(3F)+
ScannerHelper Information: 0 : (0):WriteCommand()-
ScannerHelper Information: 0 : (0):ReadResponse()+
ScannerHelper Information: 0 : (0):ReadResponse(21)-
ScannerHelper Information: 0 : (0):CheckForNewlandScanner(True)-
ScannerHelper Information: 0 : (0):IsValidNewlandScanner(True)-
ScannerHelper Information: 0 : (0):set_port()+
ScannerHelper Information: 0 : (0):set_port(34786562)-
ScannerHelper Information: 0 : (0):ScannerOn(34786562)+
ScannerHelper Information: 0 : (0):ScannerOn()-
ScannerHelper Information: 0 : (0):Open(OPOS_SUCCESS)-
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
So his method called OpenBarPort
has a try/catch around it my ScannerHelper.Open has a try catch it in for everything. What is annoying is that both his and my try/catch statements don't catch anything. But if he comments out my Open Method the problem goes away.
Here is my code for the Open method i took out the extra tracing codes for clarity.
public int Open(string portName)
{
if (portName == null)
portName = "";
if (!IsValidNewlandScanner(portName))
return OPOS_E_FAILURE;
try
{
scannerPort = new System.IO.Ports.SerialPort(portName);
scannerPort.DataReceived += scannerPort_DataReceived;
scannerPort.Open();
if (trigger is CPScannerTrigger)
{
//Sets the SafeFileHandle
(trigger as CPScannerTrigger).port = scannerPort;
}
trigger.ScannerOn();
trigger.Opened = true;
}
catch (System.Exception ex)
{
Globals.TraceError(ex);
return OPOS_E_FAILURE;
}
return OPOS_SUCCESS;
}
ScannerTrigger
public override void ScannerOn()
{
Globals.TraceStart(handle.GetHashCode());
CP210xRT_WriteLatch(handle, 4, 0);
Globals.TraceEnd();
}
public override void ScannerOff()
{
Globals.TraceStart(handle.GetHashCode());
CP210xRT_WriteLatch(handle, 4, 4);
Globals.TraceEnd();
}
public System.IO.Ports.SerialPort port
{
set
{
Globals.TraceStart();
handle = GetHandleFromSerialPort(value);
Globals.TraceEnd(handle.GetHashCode());
}
}
private Microsoft.Win32.SafeHandles.SafeFileHandle GetHandleFromSerialPort(System.IO.Ports.SerialPort sp)
{
var BaseStream = sp.BaseStream;
var baseStreamType = BaseStream.GetType();
var flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
return baseStreamType.GetField("_handle", flags).GetValue(BaseStream) as Microsoft.Win32.SafeHandles.SafeFileHandle;
}
private Microsoft.Win32.SafeHandles.SafeFileHandle handle;
So I have never see this type of null reference error. If it wasn't for that I have the possibility for 2 barcode scanners I would just mark the SafeFileHandle as static. I find it strange that the error fires IMMEDIATLY after Open returns. My barcode scanner turns on so I know the trigger "works" but i'm not sure what is causing this problem. When I run my unit test in NUnit it works with no exceptions being thrown. Can someone please render assistance? :) Thank you.