1

I have a procedure that saves the name of all of the labels that are clicked to a text file, I then need to load these names later on so that I can change the colour of them to red. This is the procedure that should load the names of the labels from the file:

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

An error occurs on the this line:

BookedSeat.Name = SeatList(intCounter)

The error says "NullReferenceException was unhandled. Object reference not set to an instance of an object." Im not sure why this is occuring, any help is appreciated. The 'Seats.txt' file contains this:

      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
Josh Cooper
  • 13
  • 1
  • 1
  • 7
  • 1
    for reference : [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/a/4660186/2998271) – har07 Feb 04 '14 at 09:26

1 Answers1

0

It sounds like the variable BookedSeat is null and when you try to access/set the .Name property, this is why you get the error. Can you set a breakpoint and try to check BookedSeat?

EDIT - As per comment

Again you can't call properties on a null object, if you want to create a new BookedSeat then do so before hand. I only know some VB but something like Dim bSeat As New BookedSeat()

Paul C
  • 4,687
  • 5
  • 39
  • 55
  • Yes the `BookedSeat` variable is null before assigning the `.Name` property to it, I didn't know that it would cause an error. So, my file contains the names of the labels that I wish to change the colour of, how would I do it then? – Josh Cooper Feb 04 '14 at 09:15
  • I have edited the question so that you can see what the 'Seats.txt' file contains. – Josh Cooper Feb 04 '14 at 09:19