0

I want to create a list of items in each room of a text adventure game, but I want each item to have both the name of the item, and the description of the item, so I can call either/both strings for the item depending on what I need the item for.

    Dim StartRoom As RoomInfo
    Public Class Items
        Public ItemName As String
        Public ItemDescription As String
    End Class
    Public Structure RoomInfo
        Dim RoomName As String
        Dim BasicDescription As String
        Dim RoomDescription As String
        Dim ValidExists As List(Of String)
        Dim ObjectsInRoom As List(Of String)

        Dim ItemsInRoom As List(Of Items)

    End Structure

    Sub InitializeRooms()

        StartRoom.RoomName = "Start Room"
        StartRoom.RoomDescription = "Test Description"
        StartRoom.BasicDescription = "Test Basic Description"
        Dim TestItem As New Items With {
            .ItemName = "Test Item",
            .ItemDescription = "This is a Test Item"
        }
        StartRoom.ItemsInRoom.Add(TestItem)


    End Sub

I've created a list, ItemsInRoom, which is a list of Items, a Class. This way, each room can have a unique list of items that can be changed as the player picks up or interacts with them. That's what the Initialize Rooms sub is for- I can run it when the game loads, and set up all the information for each room in the game. Anyway, when I try to add the TestItem to the list, I get an 'Object reference not set to an instance of an object.' error. I'm extremely dumb and this is probably the most obvious error in the book, but I can't find the solution to this particular case. Thanks for the help.

  • 2
    You need to call NEW on list before you can add items to it – CruleD Nov 05 '19 at 18:06
  • 1
    That solved it, I feel dumber but also smarter at the same time – PissMorning Nov 05 '19 at 18:14
  • `RoomInfo` should not be a structure. It should be a class and then you can include the `New` in the declaration to initialise. – jmcilhinney Nov 05 '19 at 22:02
  • You should also be declaring your types as properties rather than fields. In a class, the `List` properties should be `ReadOnly`. What that means is not that you can't add items to the `List` but rather that you can't replace the `List` with a different `List`. That's how collection properties work throughout the .NET Framework. – jmcilhinney Nov 06 '19 at 01:01
  • 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) – Stephen Kennedy Nov 06 '19 at 03:04

1 Answers1

1

This is what your type declarations ought to look like:

Public Class Item
    Public Property Name As String
    Public Property Description As String
End Class

Public Class Room
    Public Property Name As String
    Public Property BasicDescription As String
    Public Property RoomDescription As String
    Public ReadOnly Property ValidExists As New List(Of String)
    Public ReadOnly Property Objects As New List(Of String)
    Public ReadOnly Property Items As New List(Of Item)
End Class

Better names for the types and members, class rather than structure, properties rather than fields, collections created where they are declared, collection properties read-only.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46