0

I am attempting to create a list which contain a class with a list of arrays

Example: book(2).page(4).wordcount(1) = 4

so I create these:

Public Class plist
    Dim _wordcount As String = ""
    Public Property wordcount() As String
        Get
            Return _wordcount
        End Get
        Set(value As String)
            _wordcount = value
        End Set
    End Property
End Class
Public Class blist
    Dim _page As List(Of plist)
    Public Property page() As List(Of plist)
        Get
            Return _page
        End Get
        Set(value As List(Of plist))
            _page = value
        End Se
    End Property
End Class

Then I create a list like so:

Dim Book as new list(of blist)

After that everything doesn't seem right

I can add a book:

Book.add(new blist)

but I get a null reference error adding a page:

Book(0).page.add(new plist)

I am probably totally off track here. I would be very grateful if someone could put me on the right track.

RonB
  • 127
  • 2
  • 13
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – A Friend Aug 31 '17 at 14:22
  • Auto-implemented properties could reduce that boilerplate code to `Public Property Page As List(Of plist)` and initialize in the constructor (or declare them as `New`). With more idiomatic naming: `Public Property PageList As List(Of Page)` `plist` is kind of a grody class name – Ňɏssa Pøngjǣrdenlarp Aug 31 '17 at 17:01

1 Answers1

3

Your page Property backing field is currently:

Dim _page As List(Of plist) which means that the list is never initialised.

It should be

Dim _page As New List(Of plist)

A Friend
  • 2,679
  • 2
  • 11
  • 23