BUG? DataGrid + Threading = An unhandled exception of type 'System.NullReferenceException' occurred

D

DC

Hi there,

Could anyone confirm that the code below throws the following exception at
the random number of rows added:

"An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object."


'########Source Code Start###########
Imports System.Threading

Public Class Form1
Inherits System.Windows.Forms.Form
Dim m_intCount As Integer
#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents DataSet1 As System.Data.DataSet
Friend WithEvents DataTable1 As System.Data.DataTable
Friend WithEvents DataColumn1 As System.Data.DataColumn
Friend WithEvents DataColumn2 As System.Data.DataColumn
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.DataSet1 = New System.Data.DataSet
Me.DataTable1 = New System.Data.DataTable
Me.DataColumn1 = New System.Data.DataColumn
Me.DataColumn2 = New System.Data.DataColumn
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DataSet1,
System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.DataTable1,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = "Table1"
Me.DataGrid1.DataSource = Me.DataSet1
Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Fill
Me.DataGrid1.HeaderForeColor =
System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(0, 0)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(292, 266)
Me.DataGrid1.TabIndex = 0
'
'Timer1
'
'
'DataSet1
'
Me.DataSet1.DataSetName = "NewDataSet"
Me.DataSet1.Locale = New System.Globalization.CultureInfo("en-CA")
Me.DataSet1.Tables.AddRange(New System.Data.DataTable()
{Me.DataTable1})
'
'DataTable1
'
Me.DataTable1.Columns.AddRange(New System.Data.DataColumn()
{Me.DataColumn1, Me.DataColumn2})
Me.DataTable1.TableName = "Table1"
'
'DataColumn1
'
Me.DataColumn1.ColumnName = "Column1"
Me.DataColumn1.DataType = GetType(System.DateTime)
'
'DataColumn2
'
Me.DataColumn2.ColumnName = "Column2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DataSet1,
System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DataTable1,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Dim t As Thread = New Thread(AddressOf AddOneRow)
t.IsBackground = True
t.Name = m_intCount
t.Start()
End Sub

Private Sub AddOneRow()
Dim myDataTable As DataTable = Me.DataSet1.Tables(0)
Dim myRow As DataRow = myDataTable.NewRow

With myRow
.Item(0) = Now
.Item(1) = m_intCount
End With
myDataTable.Rows.Add(myRow)
m_intCount += 1
End Sub
End Class
'########Source Code End###########


Dave
 
H

Herfried K. Wagner [MVP]

* "DC said:
Could anyone confirm that the code below throws the following exception at
the random number of rows added:

"An unhandled exception of type 'System.NullReferenceException' occurred in
system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object."

Notice that instance members of Windows Forms controls are not thread
safe in general, so you will have to access them by using the control's
'Invoke' method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top