Split lines from textfile into 2 array's


I want to read lines from a file containing 2 values per line..... example of a line:

LDAP://dc=MyDomain, dc=local;MyDomain

So I read the lines into a string and split the string (on ;) and want to put the splitted values in 2 different array.

This is what I tried:

        Dim i As Integer = 0
        Dim StrLDAP As String() = Nothing
        Dim StrDomainNames As String() = Nothing

        Dim line As String
        For Each line In File.ReadAllLines(appPath & "\domains.ini")
            Dim Strvalues() As String = line.Split(New Char() {";"c})

            StrLDAP(i) = (Strvalues(0))
            StrDomainNames(i) = (Strvalues(1))
            i = i + 1


Open in new window

When I run this code I get the error "Object reference no set to an instance of an object".
I can see the StrValues are containing the splitted data so there must be something wrong with the araray's i'm putting the data in.... but what?

Please help.

Who is Participating?
Craig WagnerConnect With a Mentor Software ArchitectCommented:
Your arrays are never initialized, you declare the variable and set it to Nothing, so your array is a null reference.

Arrays don't dynamically resize so you're going to need to know how many entries the array must hold before you create them. Alternatively you could use a data structure that can be resized, such as a List(Of String). Then on each iteration of the loop you can just .Add(...) to the lists.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.