将文件加载到列表框时出现空例外。

时间:2018-06-07 22:58:23

标签: vb.net

我有问题。我正在尝试将数据加载到列表框,但是当我单击开始时,我得到一个未处理的异常,即状态值不能为空。我问题出在哪里。

我正在尝试创建一个库数据库,如果有人可以帮我解决这个问题,我真的很感激。谢谢!

Public Class frmLibrary

Structure BookData
    Dim Title As String
    Dim Author As String
    Dim ISBN As Integer
    Dim YearPublished As Integer
End Structure
Dim bookinfo() As BookData   'Array 


Private Sub frmLibrary_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim bookfiles() As String = IO.File.ReadAllLines("LibraryDatabase.txt")
    Dim n As Integer = bookfiles.Count - 1
    ReDim bookinfo(n)
    Dim line As String
    Dim books() As String   'books are the parts

    'use the split methods to assign values to the members of the structure variable. 

    For i As Integer = 0 To n  ---**>Why is this value null???????**
        line = bookfiles(i)
        books = line.Split(","c)            '
        bookinfo(i).Title = books(0)
        bookinfo(i).Author = books(1)
        bookinfo(i).ISBN = books(2)
        bookinfo(i).YearPublished = books(3)


    Next

    Dim query = From book In bookinfo
                Select book.Title, book.Author, book.ISBN, book.YearPublished
    DGVBookInfo.DataSource = query.ToList

    DGVBookInfo.Columns("Title").HeaderText = "Title"
    DGVBookInfo.Columns("Author").HeaderText = "Author"
    DGVBookInfo.Columns("ISBN").HeaderText = "ISBN"
    DGVBookInfo.Columns("Year Published").HeaderText = "Year Published"
    DGVBookInfo.AutoResizeColumns()


    DGVBookInfo.RowHeadersVisible = False
End Sub

0 个答案:

没有答案