2011-09-19 130 views
1

通常我自己處理我的錯誤,但這次我需要專家的幫助! 這從來沒有發生在我身上,並且數據越少(通常)越少,你可以說發生了什麼。VB.NET - NullReferenceException未處理

我想寫一個簡單的查詢分析器。我隨機收到這些類型的崩潰:

1)我開始用下面的函數:

Dim thd As New Thread(AddressOf StartSub) 
thd.Start() 

那麼Startsub如下:

Public Sub StartSub() 
    CheckForIllegalCrossThreadCalls = False 
    txtExecution.Text = "Executing query..." 
    Dim query As String = QueryBuilder() 
    UpdateView(query) 
End Sub 

,然後更新視圖更新數據網格我有:

Dim da As New SqlCeDataAdapter(query, connStr) 
    Dim dt As New DataTable() 
    Try 
     da.Fill(dt) 
     txtExecution.Text = "Query executed successfully." 
     dgTickets.DataSource = dt 
    Catch ex As Exception 
     txtExecution.Text = "Query failed." 
     tbGrid.BeginInvoke(Sub() tbGrid.SelectedTab = tbGrid.TabPages(1)) 
    End Try 

2)代碼在UpdateQuery中崩潰在以下李NE(調試不說,它崩潰這裏,我猜對了選擇所有線路,並通過它通過1 1去):

dgTickets.DataSource = dt 

3)什麼調試器說: 的NullReferenceException是未處理的(...) 使用new關鍵字來創建一個對象實例

堆棧跟蹤:

at System.Windows.Forms.DataGridViewCell.GetEditedFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& dataGridViewCellStyle, DataGridViewDataErrorContexts context) 
    at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) 
    at System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts) 
    at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow) 
    at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded) 
    at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded) 
    at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e) 
    at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) 
    at System.Windows.Forms.Control.WmPaint(Message& m) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.DataGridView.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.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 System.Windows.Forms.Application.Run(ApplicationContext context) 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) 
    at SQLquery.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 

這是相當模糊的實際。上面指定的文件不存在。崩潰的地方用Try-End Try包裝。此外,是的,我已經設置了繪畫事件,但它不應該關注它(或者它可能會這樣做?)。

我真的很感激任何提示,只要這一個。我必須補充一點,我使用visual basic express版本。這個錯誤會偶然發生 - 有時當我幸運的時候什麼也沒有發生,而當我不幸的時候我會發生這種崩潰。

皮特。

+0

你爲什麼故意禁用跨線程非法訪問?雖然目前還不清楚這是否是問題,但它肯定不是一個好主意... –

+0

'dgTickets'設置在哪裏? – Oded

+0

用於編寫一個完整徹底的問題! –

回答

3

你不應該觸摸/更新後臺線程中的任何GUI控件。所以線路喜歡:

txtExecution.Text = "Executing query..." 

dgTickets.DataSource = dt 

後臺線程內,是註定要失敗的。這應該始終在主GUI線程上使用Control.BeginInvoke完成。

,你似乎是在做正確

唯一的GUI更新此tbGridcatch

tbGrid.BeginInvoke(Sub() tbGrid.SelectedTab = tbGrid.TabPages(1)) 

你應該閱讀有關UI WinForms thread invokes

相關問題