VB.NET - NullReferenceException 未处理

发布于 2024-12-05 09:57:08 字数 4130 浏览 3 评论 0 原文

通常我自己处理错误,但这次我需要专家的帮助! 这从来没有发生在我身上,而且(通常)拥有的数据越少,你就越难说出发生了什么。

我正在尝试编写一个简单的查询分析器。我随机收到这些类型的崩溃:

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

然后 updateview 更新我拥有的数据网格:

    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 中崩溃(调试器没有说)它在这里崩溃了,我通过选择所有行并逐一浏览它来猜测它:

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 版本。该错误偶尔会发生 - 有时当我很幸运时什么也没有发生,而当我不幸运时,我就会遇到此崩溃。

皮特.

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2024-12-12 09:57:08

您永远不应该触摸/更新后台线程内的任何 GUI 控件。因此,像:

txtExecution.Text = "Executing query..."

dgTickets.DataSource = dt

在后台线程中这样的行注定会失败。这应该始终使用 Control.BeginInvoke 在主 GUI 线程上完成。

您似乎正确执行的唯一 GUI 更新是 catch 中的 tbGrid

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

您应该阅读有关 UI WinForms 线程调用

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文