Home > Net >  System. ArgumentNullException: 'the Value always be null. Parameter name: item'
System. ArgumentNullException: 'the Value always be null. Parameter name: item'

Time:09-18

 


Private void RefreshCom ()//a Bug: sort of way is according to the character, not Numbers, so there will be a COM10 ahead of COM4, but problem solving: just don't sort, see program!
{
CmbSerialPortIndex. Items. The Clear ();
CmbSerialPortIndex. Text=string. The Empty;

String [] ports=Win32api_findSerialPorts. MulGetHardwareInfo (Win32api_findSerialPorts. HardwareEnum. Win32_PnPEntity, "Name");//use WPI interface



If (ports!=null)
{
String [] rebuildPorts=new string [ports. Length];
For (byte I=0; I & lt; Ports. Length; I++)//the following I also feel also can optimize
{
For (byte j=0; J & lt; Ports [I]. Length; J++)
{
If ((ports [I] [j]=='(')
& & Ports [I] [m + 1]=='C'
& & (ports [I] [m + 2]=='O')
& & Ports, + 3 [j] [I]=='M'
)
{
If (ports [I] + 5 [j]==') ')//(COMn) form
{
RebuildPorts [I]="COM" + ports [I] [m + 4) + "" + ports [I] the Substring (0, j - 1);
}
Else if (ports [I] + 6 [j]==') ')//(COMnn) form
{
RebuildPorts [I]="COM" + ports [I] [j + 4] + ports [I] + 5 [j] + "" + ports [I] the Substring (0, j - 1);
}
Else if (ports + 7 [j] [I]==') ')//(COMnnn) form
{
RebuildPorts [I]="COM + ports" + + + 4 [j] [I] ports + 5 [j] + [I] ports [I] + 6 [j] + "" + ports [I] the Substring (0, j - 1);
}
Else if (ports [I] + 8 [j]==') ')//(COMnnnn) form
{
RebuildPorts [I]="COM" + ports [I] [j + 4] + ports + 5 [j] + [I] ports + 5 [j] + [I] ports + 7 [j] [I] + "" + ports [I] the Substring (0, j - 1);
}


}
}

}
CmbSerialPortIndex. Items. AddRange (rebuildPorts);
CmbSerialPortIndex. SelectedIndex=cmbSerialPortIndex. Items. Count & gt; 0? 0:1;
}
The else//almost won't appear this kind of mistake
{
MessageBox. Show (" find COM port error!" );
CmbSerialPortIndex. Text=null;

}
}
/* * * * * * * * * * * * * * * * the following functions: in order to achieve the serial port can be based on USB events to detect update serial * * * * * * * * * * * * * * * * * * */
Public const int WM_DEVICECHANGE=0 x0219;//WM_DeviceChange
Public enum WM_DEVICECHANGE_WPPARAMS
{
DBT_DEVICEARRIVAL=0 x8000,//DeviceArray
DBT_DEVICEREMOVECOMPLETE=0 x8004,//DeviceMoveComplete
};

Protected override void WndProc (ref Message m)
{
Try
{
If (m.M sg==WM_DEVICECHANGE)
{
The switch ((WM_DEVICECHANGE_WPPARAMS) (m. Param. ToInt32 ()))
{
Case WM_DEVICECHANGE_WPPARAMS. DBT_DEVICEARRIVAL:
Tmr_StrartRefreshCom. Enabled=Enabled;
break;
Case WM_DEVICECHANGE_WPPARAMS. DBT_DEVICEREMOVECOMPLETE://unloading
Tmr_StrartRefreshCom. Enabled=Enabled;
break;
Default:
break;
}
}
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message);
}
Base. WndProc (ref m);
}







When I want to perform, we don't understand how can appear System. ArgumentNullException: 'the Value always be null.
The Parameter name: item '

I am using Visual studio 2019,














CodePudding user response:

Debugging, behold a line error, including variable parameter is null

CodePudding user response:

reference 1/f, guiyang horse Ma Shanfu plugging waterproof engineering professional maintenance of swimming pool response:
debugging, behold a line error, including the variable parameter is null


After debugging found are: cmbSerialPortIndex. Items. AddRange (rebuildPorts); An error,

CodePudding user response:

reference 1/f, guiyang horse Ma Shanfu plugging waterproof engineering professional maintenance of swimming pool response:
debugging, behold a line error, including the variable parameter is null


Can you help me to see? In fact, I won't c #, thank you

CodePudding user response:

You can help me have a look? Was not good at c #
  •  Tags:  
  • C#
  • Related