Imports System.IO
Imports System
Public Class Form1
Private Class movie
Public name As String
Public actors() As String
Public year As String
Public country As String
Public votes As String
End Class
Private movies(0) As movie
Private fs As FileStream
Private input As StreamReader
Private Sub LoadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadToolStripMenuItem.Click
OpenFileDialog1.ShowDialog()
Dim i As Integer = 0
Dim currentmv As New movie
Dim reader As String()
If File.Exists(OpenFileDialog1.FileName) Then
lblPath.Text = OpenFileDialog1.FileName
Dim iFile As New StreamReader(lblPath.Text)
While Not iFile.EndOfStream
reader = iFile.ReadLine.Split(";")
currentmv.name = reader(0)
currentmv.actors = reader(1).Split(",")
currentmv.year = reader(2)
currentmv.country = reader(3)
currentmv.votes = reader(4)
lbMovies.Items.Add(currentmv.name)
movies(i) = currentmv
i = i + 1
ReDim movies(i)
End While
End If
End Sub
Private Sub lbMovies_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbMovies.SelectedIndexChanged
Dim i As Integer
For i = 0 To movies(lbMovies.SelectedIndex).actors.Length
lbActors.Items.Add(movies(lbMovies.SelectedIndex).actors(i))
Next
End Sub
End Class
Problem strikes when I request the length. It says it’s a null reference, but I cannot see any mistake in this. Is there an issue when I request the length of the array in an array of objects?
Try
IIRC
ReDimwill initialize all elements toNothingwithoutPreserve.