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 :
我是vb.net编程的新手,我想从文件中读取一个二维数组。我搜索了很多,我无法弄清楚我该怎么做。有输入文件:
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
WindowsApplication1.exe中发生了未处理的“System.NullReferenceException”类型异常
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.
我真的不知道出了什么问题。
2 个解决方案
#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()
#2
You are reading too many lines...if there is no line to read, a Null reference is returned by ReadLine.
您正在读取太多行...如果没有要读取的行,则ReadLine将返回Null引用。
You need to ReadLine from 0 to 10, and for each line, use split to get the column values.
您需要从0到10的ReadLine,并且对于每一行,使用split来获取列值。
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.
它会导致异常。 linie变量为null。
#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()
#2
You are reading too many lines...if there is no line to read, a Null reference is returned by ReadLine.
您正在读取太多行...如果没有要读取的行,则ReadLine将返回Null引用。
You need to ReadLine from 0 to 10, and for each line, use split to get the column values.
您需要从0到10的ReadLine,并且对于每一行,使用split来获取列值。
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.
它会导致异常。 linie变量为null。