1

I already make the code but there's a problem. The compiler show me this message:

Object reference not set to an instance of an object. This is the code:

Dim array As List(Of String)
For Each row As DataGridViewRow In DataGridView1.Rows
        If Not row.IsNewRow Then
            array.Add(row.Cells(0).Value.ToString & "," & row.Cells(1).Value.ToString & _
                            row.Cells(2).Value.ToString & "," & row.Cells(3).Value.ToString)
        End If
    Next

The line with the problem is array.add

What's the problem?

  • 4
    You didn't instantiate the List. Change `Dim array As List(Of String)` to `Dim array As New List(Of String)`. Horrible variable name by the way... – Idle_Mind Dec 17 '14 at 19:22
  • Or try using Dim Array As New ArrayList(). Then use array.add() – Dman Dec 17 '14 at 21:27

1 Answers1

0

When you use a list, you have to create an instance of it using New:

Dim array As New List(Of String)

Like Idle_Mind mentioned, it's a bad idea to use Array as a variable name. It's confusing and makes the code hard to read.

xpda
  • 15,585
  • 8
  • 51
  • 82