0

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.

Robert Snyder
  • 2,399
  • 4
  • 33
  • 65
  • Only thing I see is that he sets the port name to "" if its null then does a test to see if its a valid scanner I would take a look in that method also cause I imagine hes trying to find a port based off the name "". Could you post IsValidNewlandScanner code? – Bearcat9425 Jul 16 '13 at 15:52
  • @Bearcat9425 I sure can. The Open code is mine. I put in a empty string instead of null for the IsValidNewlandScanner code. That part passes otherwise the scanner would never turn on. The trace shows that COM6 was passed into my method. – Robert Snyder Jul 16 '13 at 15:54
  • Code that works in test but that fails in Production is a symptom of race conditions. Consult http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net – Dour High Arch Jul 16 '13 at 15:54
  • Blah ignore my comment i didn't look at the stack trace apparently. – Bearcat9425 Jul 16 '13 at 15:58
  • Does the exception contain any inner exceptions? Also, is it possible that the scannerPort_DataReceived method is throwing the exception? – sgmoore Jul 16 '13 at 17:17
  • I'm not sure how to check the inner exception. The data received does work (providing i can scan a barcode fast enough) I remember having fits with this now a long time ago. Somethign to do with the SafeFileHandle.. I'm almost positive that is what it is. I read the documentation on the scanner and it turns out I can change the reading mode by writing to the serial port. I'm going to try that. If it works it will remove 3 needed libraries and 2 classes :) – Robert Snyder Jul 16 '13 at 17:23

0 Answers0