Null reference but it's not?

Posted by Clint Davis on Stack Overflow See other posts from Stack Overflow or by Clint Davis
Published on 2010-06-02T17:10:16Z Indexed on 2010/06/02 17:14 UTC
Read the original article Hit count: 153

This is related to my previous question but it is a different problem.

I have two classes: Server and Database.

Public Class Server

Private _name As String
Public Property Name() As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
    End Set
End Property


Private _databases As List(Of Database)
Public Property Databases() As List(Of Database)
    Get
        Return _databases
    End Get
    Set(ByVal value As List(Of Database))
        _databases = value
    End Set
End Property


Public Sub LoadTables()
    Dim db As New Database(Me)
    db.Name = "test"

    Databases.Add(db)

End Sub
End Class

Public Class Database
Private _server As Server

Private _name As String
Public Property Name() As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
    End Set
End Property


Public Property Server() As Server
    Get
        Return _server
    End Get
    Set(ByVal value As Server)
        _server = value
    End Set
End Property


Public Sub New(ByVal ser As Server)
    Server = ser
End Sub
End Class

Fairly simple. I use like this:

Dim s As New Server
    s.Name = "Test"
    s.LoadTables()

The problem is in the LoadTables in the Server class. When it hits Databases.Add(db) it gives me a NullReference error (Object reference not set). I don't understand how it is getting that, all the objects are set. Any ideas? Thanks.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about vb.net