【问题标题】:Connecting to Com5: System.NullReferenceException was unhandled连接到 Com5:未处理 System.NullReferenceException
【发布时间】:2012-09-30 10:54:05
【问题描述】:

我正在尝试使用以下 c# 代码以每秒 250000 位的波特率访问我的 COM5 端口,但在 selectedbaudrate = Convert.ToInt32 (cmbBaudRate.SelectedItem.ToString()); 行收到下面的异常消息。使用pronterface.py 程序,我可以以这种速度访问我的设备。为什么我写250000 时在selectedbaudrate 变量上得到0

private void buttonOpenPort_Click(object sender, EventArgs e)                                   
{
    string selectedportname;                                                                    
    int selectedbaudrate;
    selectedportname = combportnumber.SelectedItem.ToString();                                  
    selectedbaudrate = Convert.ToInt32 (cmbBaudRate.SelectedItem.ToString());                  
    port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One);    
    port.DtrEnable = true;
    port.Open();                                                                               
    port.DataReceived += serialPort1_DataReceived;    
}

例外:

System.NullReferenceException was unhandled
      Message=Object reference not set to an instance of an object.
      Source=PC Host
      StackTrace:
           at WindowsFormsApplication1.Form1.buttonOpenPort_Click(Object sender, EventArgs e) in D:\Catia V5 Projects\HF08 Hexapod\Version 05\Host PC Software\PC Host 13\PC Host\Form1.cs:line 106
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at WindowsFormsApplication1.Program.Main() in D:\Catia V5 Projects\HF08 Hexapod\Version 05\Host PC Software\PC Host 13\PC Host\Program.cs:line 18
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

【问题讨论】:

  • 您是否在调试器中单步执行代码来检查变量的值?
  • ChrisF,是的,奇怪的是,当我指定 250000 时,selectedbaudrate 为 0。
  • 那么问题出在应该设置selectedBaudRate的代码中,或者在设置后它被覆盖了。
  • 谢谢克里斯,我该如何解决这个问题?
  • @Arthur Mamou-Mani:“null”并不意味着“零”。 null 表示应该“指向”一个对象的变量当前“指向”什么都没有。

标签: c# python exception serial-port device


【解决方案1】:

cmbBaudRate 或 cmbBaudRate.SelectedItem 为空

【讨论】:

  • 为什么我指定 250000 时它为空?
  • cmbBAudRate 我猜是一个组合框。执行代码时可能尚未创建它(== cmbBaudRate 为空)。或者可能当时没有选择组合框中的任何项目(== cmbBaudRAte.SelectedItem 为空)
  • @Arthur Mamou-Mani:我感觉您误解了 NullReferenceException。这并不意味着该值为零,这意味着您正在尝试使用对对象的未初始化引用。
  • 谢谢,我确实把他们弄糊涂了,不知道uninitialized reference to an object。我会调查的。当我调试时,cmdBaudRate 变量被定义为 0,这是一个错误,因为我指定了某个数字。我认为该框根本无法识别我手动输入的值。现在可以使用了。
【解决方案2】:

对不起,简单的解决方案:我已将值 250000 添加到表单中的下拉值中并且它有效。

【讨论】:

    猜你喜欢
    • 2018-09-03
    • 2013-09-04
    • 2013-04-25
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多