1

Resolved

1.1.4.0 GetPortDescriptions() NullReferenceException

description

I was attempting to enumerate available serial ports, and tried first without any USB-to-serial adapters (I'm using FTDI adapters - one built into a cable and one built into a PIC MCU demo/eval board) plugged in. GetPortDescriptions() threw an unhandled System.NullReferenceException at the line:
string[] k = local.GetValueNames();
due, I assume, to the Registry key HKLM\HARDWARE\DEVICEMAP\SERIALCOMM not being present so "local" was null? Exception details:
System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=RJCP.SerialPortStream
  StackTrace:
       at RJCP.IO.Ports.SerialPortStream.GetPortDescriptions() in R:\Src\Comms\PC_Serial\SerialPortStream\SerialPortStream.cs:line 515
       at Csharp_Console.Program.SerialPortsEnumerate() in R:\Src\Comms\PC_Serial\Csharp_Console\Program.cs:line 31
       at Csharp_Console.Program.Main(String[] args) in R:\Src\Comms\PC_Serial\Csharp_Console\Program.cs:line 20
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
My Registry indeed appears to have no SERIALCOMM key with no FTDI USB-to-serial adapters plugged in, but has a populated SERIALCOMM key with FTDI USB-to-serial adapter(s) plugged in.

file attachments

comments

jmcurl wrote Sep 26, 2015 at 5:50 PM

Thankyou for the problem report. You are correct - the API Registry.OpenSubKey() returns null if the key doesn't exist. This wasn't being checked. The solution is so simple that I just check for null.

wrote Sep 26, 2015 at 5:53 PM

jmcurl wrote Sep 26, 2015 at 5:59 PM

Please test against r33068 that contains the fix.

AlexWITEE wrote Sep 26, 2015 at 9:27 PM

Tested against r33068 - no exception thrown any more, and can easily test for empty array to determine that no ports are present in the system. Fixed.

jmcurl wrote Sep 28, 2015 at 8:16 AM

confirmed as fixed - moved to resolved. Will be in the next release.