[SOLVED] BlinkStickDotNet UsbMonitor NullReferenceException on Connect/Disconnect

I have a .NET application utilizing the BlinkStickDotNet library, and I need to be able to react when the device is disconnected and connected. I found the UsbMonitor class has events for that (BlinkStickConnected and BlinkStickDisconnected, perfect!) but whenever the device is disconnected, a NullPointerException occurs in UsbMonitor.cs (line 100):

100 foreach (BlinkStick device in devices)
101 {
102 OnBlinkStickDisconnected(device);
103 }

because “devices” is null. The same error occurs if I start the application (which includes a UsbMonitor object with event handlers assigned to both events) without the device connected then connect it.

Ha! Turns out I’m just an idiot. There’s a Start() method on the UsbMonitor that I needed to be calling.

Hi Steve,

I don´t want to miss to welcome you to the forums.

And don´t be too hard to yourself :slight_smile:

Hi Steve, 

This is what I did for handling Insert and remove events. 

using HidLibrary;

class clsBlinkStick
{
    private HidDevice m_stick;
    public event DeviceInsert DInsert;
    public delegate void DeviceInsert(string sEqpID, EventArgs e);

    public event DeviceRemove DRemove;
    public delegate void DeviceRemove(string sEqpID, EventArgs e);
    public void Init(HidDevice device)
    {
        m_stick = device;     
        m_stick.Removed += new HidDevice.RemovedEventHandler(clsBlinkStick_Remove);
        m_stick.Inserted += new HidDevice.InsertedEventHandler(clsBlinkStick_Insert);     
    }
    private void clsBlinkStick_Insert()
    {
     //Code here
    }
    private void clsBlinkStick_Remove()
    {
     //Code here
    }
 }

 Thanks

 Rick