Avatar of ROM
ROM
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Issue when creating reusable code. Stated works fine. When a variable produces: Object Ref not set to an instance of an object

VB application that sends PowerShell commands and receives the output.

I am putting the output in to a List view.

The following works fine:

 
[code]      For Each ID In PSOut

            i = i + 1

            'Dim str(6) As String
            Dim str(ColumnCount) As String
            Dim ItCount As Integer = 0
            Dim itm As ListViewItem

            '     Do While ItCount < ColumnCount
            For Each strHeadDSingle In strHeadD2
                str(ItCount) = ID.Properties("name").Value.ToString() ' ### WORKS FINE
                ItCount = ItCount + 1
            Next
            '    Loop
            '         Name, SamAccountName, EmailAddress, PrimarySMTPAddress, UserPrincipalName, DistinguishedName, Enabled"

            itm = New ListViewItem(str)
            ListView1.Items.Insert(0, itm)
        Next

Open in new window

[/code]


When passed as a variable I get the following:

  For Each ID In PSOut

            i = i + 1

            'Dim str(6) As String
            Dim str(ColumnCount) As String
            Dim ItCount As Integer = 0
            Dim itm As ListViewItem

            '     Do While ItCount < ColumnCount
            For Each strHeadDSingle In strHeadD2
                str(ItCount) = ID.Properties(strHeadDSingle).Value.ToString()          ' ### ERRORS THIS LINE
                ItCount = ItCount + 1
            Next
            '    Loop
            '         Name, SamAccountName, EmailAddress, PrimarySMTPAddress, UserPrincipalName, DistinguishedName, Enabled"

            itm = New ListViewItem(str)
            ListView1.Items.Insert(0, itm)
        Next

Open in new window

     


it works 100% as a stated item.

When I try to make my reusable code I get the following on this line when debugging:

System.NullReferenceException
Object reference not set to an instance of an object.

I have checked and values are there quite happily... but little confused.,

Please advise

Many thanks in advance

R
PowershellStatistical PackagesVisual Basic.NETVB ScriptR

Avatar of undefined
Last Comment
ROM

8/22/2022 - Mon