开发者

VB.NET - NullReferenceException Unhandled

Usually I handle my bugs by myself but this time I need help of experts! This never happened to me, and the less data one has (usually) the less you can say what happened.

I am trying to write a simple query analyser. I randomly receive these kind of crashes:

1) I am starting with the following function:

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

then the Startsub follows:

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

and then the updateview updates the datagrid I have:

    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) The code crashes in UpdateQuery on the following line (debugger does not say that it crashes here, I guessed it by selecting all lines and going through it 1 by 1):

dgTickets.DataSource = dt

3) What the debugger says: NullReferenceException开发者_JS百科 was unhandled(...) use the new keyword to create an object instance

stack trace:

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 

This is pretty vague actually. The file specified above does not exist. The place where it crashes is wrapped with Try-End Try. Moreover, yes, I have the painting event set up but it should not concern it (or maybe it does?).

I would really appreciate very very much any hints as far as this one. I have to add that I use visual basic express edition. The error occurs ocasionally - sometimes when I'm lucky nothing happens, and when I'm not then I get this crash.

Pete.


You should never touch/update any GUI controls inside background threads. So lines like:

txtExecution.Text = "Executing query..."

and

dgTickets.DataSource = dt

inside a background thread are doomed to fail. This should always be done on the main GUI thread using Control.BeginInvoke.

The only GUI update that you seem to be doing correctly is this tbGrid in the catch:

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

You should read about UI WinForms thread invokes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜