2011-09-19 76 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