我对 VB.Net 有点陌生。我的问题是代码给出了System.NullReferenceException。我不知道我的错在哪里,我一直在寻找整个代码以查找错误 2 小时。这是文本文件和代码
E:\源\胶片\数据存储.mdf
endgame Avengers:_End_Game_(2019) Indonesia Action_Adventure_Drama
ageofultron Avengers:_Age_of_Ultron_(2015) Indonesia Action_Adventure_Sci-Fi
在"公共类"部分
Private ReadOnly Path As String = "E:SourceFilm"
Private Shared FolderData() As String
Private Shared Title As String
Private Shared Subtitle As String
Private Shared Genre As String
这在私人子部分
Dim FilmData(File.ReadAllLines(Path + "datastore.mdf").Length) As String
Dim FileNum = FreeFile()
FileOpen(FileNum, Path + "datastore.mdf", OpenMode.Input)
Dim x As Integer = 0
Do Until (EOF(FileNum))
FilmData(x) = LineInput(FileNum)
x += 1
Loop
FileClose(FileNum)
If FilmData(0) IsNot Nothing Then
Dim Data1() As String = FilmData(0).Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
FolderData(0) = Data1(0)
Title = Data1(1)
Title = Title.Replace("_", " ")
Subtitle = Data1(2)
Genre = Data1(3)
Genre = Genre.Replace("_", " ")
End If
代码给了我 System.NullReferenceException 上Title = Data1(1)
行也是字幕和流派
任何解决方案或有人可以找到原因在哪里?任何回应和答复将不胜感激
你的问题出在FolderData
上。你把它声明为Private Shared FolderData() As String
,但你没有给它一个维度。不能将项添加到没有维度的数组中。由于我们可能不知道数组的大小,因此我将其更改为List(Of String)
并使用列表的.Add
方法。
Private Sub OPCode()
Dim FilmData = File.ReadAllLines(Path.Combine(DataPath, "datastore.mdf"))
If FilmData(0) IsNot Nothing Then
Dim Data1 = FilmData(0).Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
FolderData.Add(Data1(0))
Title = Data1(1)
Title = Title.Replace("_", " ")
Subtitle = Data1(2)
Genre = Data1(3)
Genre = Genre.Replace("_", " ")
End If
Debug.Print($"The Folder Date is {FolderData(0)}")
Debug.Print($"The Title is {Title}")
Debug.Print($"The Subtitle is {Subtitle}")
Debug.Print($"The Genre is {Genre}")
End Sub
即时窗口显示
The Folder Date is endgame
The Title is Avengers: End Game (2019)
The Subtitle is Indonesia
The Genre is Action Adventure Drama
我不确定为什么您的变量被声明为Private Shared
.如果它们是私有的,那么它们在课堂之外就看不到,那么,为什么要共享它们呢?