Vb.net 从文本文件加载对象名称(Visual Basic)
我有一个程序,将所有单击的标签的名称保存到文本文件中,然后我需要稍后加载这些名称,以便我可以将它们的颜色更改为红色。这是从文件中加载标签名称的过程:Vb.net 从文本文件加载对象名称(Visual Basic),vb.net,visual-studio-2010,list,file,text-files,Vb.net,Visual Studio 2010,List,File,Text Files,我有一个程序,将所有单击的标签的名称保存到文本文件中,然后我需要稍后加载这些名称,以便我可以将它们的颜色更改为红色。这是从文件中加载标签名称的过程: Sub LoadSeats() If My.Computer.FileSystem.FileExists("Seats.txt") = True Then 'Checks if the seats.txt file is present FileReader = New StreamReader("Seats.txt")
Sub LoadSeats()
If My.Computer.FileSystem.FileExists("Seats.txt") = True Then 'Checks if the seats.txt file is present
FileReader = New StreamReader("Seats.txt")
NumOfBookedSeats = FileReader.ReadLine() 'Finds out how many seats have been booked
For intCounter = 1 To NumOfBookedSeats
SeatList.Add(FileReader.ReadLine)
BookedSeat.Name = SeatList(intCounter)
BookedSeat.BackColor = Color.Red
BookedSeat.ForeColor = Color.Red
Next
Else
FileWriter = New StreamWriter("Seats.txt")
FileWriter.WriteLine(0)
FileWriter.Close()
End If
End Sub
此行上出现错误:
BookedSeat.Name = SeatList(intCounter)
错误为“NullReferenceException未处理。对象引用未设置为对象的实例”。我不确定发生这种情况的原因,请提供帮助。
“Seats.txt”文件包含以下内容:
4 'This is the number of names in the file
lblG1 'This line and every one following it will contain the name of a label
lblH1
lblI1
lblJ1
听起来变量
BookedSeat
为空,当您尝试访问/设置.Name
属性时,这就是出现错误的原因。能否设置断点并尝试检查BookedSeat
编辑-根据评论
同样,您不能对空对象调用属性,如果您想创建一个新的
BookedSeat
,请在创建之前执行。我只知道一些VB,但类似于Dim bSeat As New BookedSeat()
是的BookedSeat
变量在分配.Name
属性之前为空,我不知道它会导致错误。因此,我的文件包含我希望更改颜色的标签名称,我将如何更改?我已编辑了问题,以便您可以查看“Seats.txt”文件包含的内容。供参考: