mandelbug
mandelbug

Reputation: 1798

VB.net classes - Object reference not set to an instance of an object?

I'm working on an application in VB that is giving me some trouble. Coming from Java and C++, the class syntax for VB is peculiar. I have my main form, and a class I created called WebElement. I imported the class to MainForm.vb and declared an array of WebElement's. When I try to set or get the Name attribute of the first element of my array of 'WebElement`'s, it gives me an error - "Object reference not set to an instance of an object?" What does this mean, and how do I fix it?

Code
MainForm.vb

Imports MyProgram.WebElement

Public Class MainForm

    Private webpage(0 To 9) As WebElement
    Private pageNum As Integer = 0

    Private Sub openFile() Handles OpenToolStripMenuItem.Click
        webpage(pageNum).setName("rawr")
        MsgBox(webpage(pageNum).getName())
    End Sub

End Class

WebElement.vb

Public Class WebElement

    Private Name As String

    Public Function setName(ByRef n As String)
        Name = n
    End Function

    Public Function getName()
        Return Name
    End Function

End Class

Upvotes: 0

Views: 7516

Answers (5)

Steve
Steve

Reputation: 216293

This line

 Private webpage(0 To 9) As WebElement

declares an array of 10 elements that should be of type WebElement.
No element is present in the array. So every slot is Nothing (null in C#).
Calling a method on a null element will give the NullReferenceException

You should check you element before calling the method and, if it is null, create the element and assign it to the required slot

Private Sub openFile() Handles OpenToolStripMenuItem.Click
    if webpage(pageNum) Is Nothing Then
       webpage(pageNum) = new WebElement()
    End If
    webpage(pageNum).setName("rawr")
    MsgBox(webpage(pageNum).getName())
End Sub

As a side note, why don't you try to use the NET syntax to implement class properties

Public Class WebElement

    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
End Class

and then use it in your code

 webpage(pageNum).Name = "rawr"
 MessageBox.Show(webpage(pageNum).Name)

Upvotes: 1

Cody Gray
Cody Gray

Reputation: 244722

The class syntax in VB.NET may be peculiar, but the usage of classes is very similar to Java and C++.

In particular, there is a difference between a definition of a class type (which you've written in WebElement.vb and imported into your MainForm.vb code file), and an object of that class type. The latter is the one that you're missing.

You need to create (instantiate) an object of the WebElement class type in your code. The array you have declared right now is empty (i.e., all of its elements are null). All you've done is declared it. The compiler doesn't create and fill it with objects until you ask it to do so.

You need to initialize the elements in the array with a new object. You do this by using the New keyword. The syntax looks like this:

webpage(0) = New WebElement()   ' initializes the first element in the array
                                ' with a new WebElement object

You can also initialize the array elements inline when you declare it, if you so choose. In order to make this magic happen, you omit the size from the left side of the declaration and use an initializer list on the right, like so:

Dim webpage() As WebElement = {
                               New WebElement(),
                               New WebElement(),
                               New WebElement(),
                               New WebElement(),
                               New WebElement(),
                               New WebElement(),
                               New WebElement(),
                               New WebElement(),
                               New WebElement(),
                               New WebElement()
                              }

But this syntax gets pretty unwieldy for long arrays, so most people prefer looping over the elements of the array immediately after declaration and creating the objects.

Upvotes: 2

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25435

You don't fill your array with WebElements, you only tell it what size it needs to be. So webpage(pageNum) is a null object.

Try

Private Sub openFile() Handles OpenToolStripMenuItem.Click
    webpage(pageNum) = New WebElement()
    webpage(pageNum).setName("rawr")
    MsgBox(webpage(pageNum).getName())
End Sub

Upvotes: 2

Mitch Bukaner
Mitch Bukaner

Reputation: 181

That means NullPointerException because the objects haven´t been initialized, just like in java.

try

Private webpage(0 To 9) As New WebElement

Upvotes: 0

Zdeslav Vojkovic
Zdeslav Vojkovic

Reputation: 14581

You have created webpage array, but all of its elements are null, so webpage(pageNum).setName("rawr") dereferences a null object

Upvotes: 0

Related Questions