2012-09-30 39 views
1

我想以每秒250000位的波特率訪問我的COM5端口,但使用以下c#代碼,但在行selectedbaudrate = Convert.ToInt32 (cmbBaudRate.SelectedItem.ToString());處獲得以下異常消息。使用pronterface.py程序,我可以以此速率訪問我的設備。當我寫250000時,爲什麼我會在selectedbaudrate變量上獲得0連接到Com5:System.NullReferenceException是未處理的

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: 
+1

你是否已經通過調試器中的代碼檢查變量的值? – ChrisF

+0

ChrisF,是的奇怪的是,當我指定250000時'selectedbaudrate'爲0。 –

+1

然後問題在於應該設置'selectedBaudRate'的代碼,或者在設置它後它會被覆蓋。 – ChrisF

回答

0

對不起球員,簡單的解決方案:我在形式的值250000加到下拉值和它的工作。

1

要麼cmbBaudRate或cmbBaudRate.SelectedItem爲null

+0

雖然我指定了250000,但它爲什麼爲空? –

+0

cmbBAudRate是,我想是一個組合框。當你的代碼執行時它可能還沒有創建(== cmbBaudRate爲null)。或者,當時可能沒有選擇組合框中的項目(== cmbBaudRAte.SelectedItem爲空) –

+0

@Arthur Mamou-Mani:我覺得你錯誤地解釋了NullReferenceException。這並不意味着該值爲零,這意味着您正試圖對對象使用未初始化的引用。 –

相關問題