Question

I have a multidimensional array which will hold 3 bits of value and value will be added through a loop by reading from db. When it come this line of code:

*MenuAccess(.Fields("GROUPNO").Value, .Fields("ITEMNO").Value, .Fields("SUBITEMNO").Value) = OKNO*

It's hitting error, 'Object reference not set to an instance of an object.'

Public MenuAccess(100, 30, 50) As Boolean

Private Sub RtGetMenu_Access()
    Try
        Dim OKNO As Boolean

        Erase MenuAccess

        DBMgr = New cAdodb

        With DBMgr
            .openCnn(cnnSPR)
            .sql(rst1) = "Select * from BSMENU_ACCESS where LEVELCD = '" & Trim(UserTaskLevel) & "' Order by GROUPNO "
            .openRstRead(rst1)

            With .rst(rst1)
                Do While Not .EOF
                    If .Fields("GROUPNO").Value.ToString.Trim <> "" And .Fields("ITEMNO").Value.ToString.Trim <> "" And .Fields("SUBITEMNO").Value.ToString.Trim <> "" Then
                        If .Fields("LEVEL1").Value.ToString.Trim = "Y" Then OKNO = True Else OKNO = False

                        '=> ERROR: 'Object reference not set to an instance of an object.'
                        MenuAccess(.Fields("GROUPNO").Value, .Fields("ITEMNO").Value, .Fields("SUBITEMNO").Value) = OKNO

                    End If
                    .MoveNext()
                Loop
            End With

            .closeRst(rst1)
            .closeCnn(rst1)
        End With

        DBMgr = Nothing
    Catch ex As Exception
        If DBMgr.cnn(cnnSPR).State <> 0 Then
            DBMgr.closeCnn(cnnSPR)
            DBMgr = Nothing
        End If
    End Try

End Sub
Was it helpful?

Solution

You are missing an initialization for the array

Public MenuAccess(100, 30, 50) As Boolean   'Object not initialized in VB.NET

Try this

Public MenuAccess() As New Boolean(100,30,50)

Also convert the fields into integers with Integer.TryPrase()

Dim i as Integer=-1, j as Integer=-1, k as Integer=-1
Integer.TryParse(.Fields("GROUPNO").Value.ToString(), i)
Integer.TryParse(.Fields("ITEMNO").Value.ToString(), j)
Integer.TryParse(.Fields("SUBITEMNO").Value.ToString(), k)

If i>=0 AndAlso j>=0 AndAlso k>=0 Then
    MenuAccess(i,j,k) = OkNo
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top