简体   繁体   中英

C# to VB.net Invoke error

Hey all I am trying to figure out how to get the code below to work in VB.net. I already converted it from C# to VB.net:

Dim dt As New DataTable()
Dim dr As DataRow

dt.Columns.Add("Name")
dt.Columns.Add("Number")

For Each item As KeyValuePair(Of String, String) In vCardReader.dicNumName
    dr = dt.NewRow()
    dr(0) = item.Value
    dr(1) = item.Key
    dt.Rows.Add(dr)
Next

Me.Invoke(New MethodInvoker(
 Sub()
          contactNameNumList.DataSource = dt
          contactNameNumList.AllowUserToResizeRows = False
          contactNameNumList.AllowUserToResizeColumns = False
          contactNameNumList.AllowUserToOrderColumns = False
          contactNameNumList.CellBorderStyle = DataGridViewCellBorderStyle.None
          contactNameNumList.Columns(0).Width = 30
          contactNameNumList.Columns(1).Width = contactNameNumList.Width / 2 - 25
          contactNameNumList.Columns(2).Width = contactNameNumList.Width / 2 - 25
End Sub))

However, when I run the program, the error I get for the above code is this:

Object reference not set to an instance of an object.

How can I correct this error?

在此处输入图片说明

How can I correct this error?

The short answer make sure your objects are initialized. I'll assume contactNameNumList is a DataGridView and dt is a DataTable . If you created either or both of these in code make sure you used the New keyword or initialized them with real data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM