-2

I'm trying to create a functional class with some property but it keeps saying

Object reference not set to an instance of an object.

Public Class Class1    
    Private sParam As Dictionary(Of String, String)

    Public Property param As Dictionary(Of String, String)
        Set(ByVal value As Dictionary(Of String, String))
            sParam = value
        End Set
        Get
            Return sParam
        End Get
    End Property

    Public Sub insert(ByVal table As String)
        Dim col As String = ""
        Dim val As String = ""
        Dim init As Integer = 0

        For Each x In param
            If init = 0 Then
                col &= x.Key
                val &= x.Value
                init = 1
            Else
                col &= ", " & x.Key
                val &= ", " & x.Value
            End If
        Next

        MsgBox("INSERT INTO " & table & "(" & col & ") VALUES (" & val & ")")
    End Sub
End Class
Public Class Form1
    Private Sub Button1_Click(ByVal sender As Button, ByVal e As System.EventArgs) Handles Button1.Click
        Dim db As Class1
        db = New Class1
        db.param.Add("First_name", "John")
        db.param.Add("Last_name", "Doe")
        db.insert("tblUsers")
    End Sub
End Class
InteXX
  • 6,135
  • 6
  • 43
  • 80

1 Answers1

0

You need to instantiate your Dictionary(Of String, String) object:

Public Class Class1    
    Private sParam As New Dictionary(Of String, String)
                      '''
    ...

End Class
InteXX
  • 6,135
  • 6
  • 43
  • 80