-1

I'm new in vb.net programming, and i want to read a 2d array from a file. I searched a lot and i can't figure out how can i do that. There is the input file :

1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1

And here is the code part :

Dim map As Integer(,)
Dim reader As StreamReader
    reader = IO.File.OpenText(folder + "\harta\harta.txt")
    Dim linie As String, i, j As Integer
    For i = 0 To 10
        For j = 0 To 12
            linie = reader.ReadLine()
            map(i, j) = linie.Substring(j, linie.IndexOf(" ")) 'here is my problem'
        Next j
    Next i
    reader.Close()

When i run the code, i get the following error:

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

Edit:

I tried another method :

Dim reader As IO.StreamReader
    reader = IO.File.OpenText(folder + "\harta\harta.txt")
    Dim linie As String, i, j As Integer
    For i = 0 To 10
        linie = reader.ReadLine
        Dim parametrii As String() = linie.Split(" ")
        Dim parametru As String
        j = 0
        For Each parametru In parametrii
            map(i, j) = parametru 'i get the same error here'
            j += 1
        Next
    Next i

I really dont know what is wrong.

spongebob
  • 8,370
  • 15
  • 50
  • 83
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Daniel A. White May 09 '15 at 13:05
  • 2
    Aside: there are 10 lines there, but each line doesnt also have 12 lines in it. you should split each line to get the "columns". – Ňɏssa Pøngjǣrdenlarp May 09 '15 at 13:07

2 Answers2

1

Here you are, and I fixed some problems that you can see by comparing between this code and yours :

    Dim map(10, 12) As Integer
    Dim reader As IO.StreamReader
    reader = IO.File.OpenText("harta.txt")
    Dim linie As String, i, j As Integer
    For i = 0 To 10
        linie = reader.ReadLine.Trim
        For j = 0 To 12
            map(i, j) = Split(linie, " ")(j)
        Next j
    Next i
    reader.Close()
Top Systems
  • 951
  • 12
  • 24
  • Still getting that error: "An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe Additional information: Object reference not set to an instance of an object." – Cătălin Muntean May 09 '15 at 13:39
  • Did you try my code as is? may be you defined the map as you wrote before `Dim map As Integer(,)` this may return that error. And don't forget to set the right path for your file. – Top Systems May 09 '15 at 13:43
  • I tryed your code as is, and the map is declared as and String `Dim map As String(,)` – Cătălin Muntean May 09 '15 at 13:53
  • Change it to `Dim map(10, 12) As String` – Top Systems May 09 '15 at 13:59
0

You are reading too many lines...if there is no line to read, a Null reference is returned by ReadLine.

You need to ReadLine from 0 to 10, and for each line, use split to get the column values.

This part is currently returning a null reference:

linie = reader.ReadLine()

And when you attempt this:

linie.IndexOf(" ")

It causes an exception. The linie variable is null.

trucker_jim
  • 572
  • 3
  • 7