0

I am working in VB.net where i have class like below:

Public Class vertex
    Public wasVisited As Boolean
    Public name, type As String
    Public x_pos, y_pos As Double

    Public Sub New(ByVal x_pos As Double, ByVal y_pos As Double, ByVal name As Integer, ByVal type As String)
        Me.x_pos = x_pos
        Me.y_pos = y_pos
        Me.name = name
        Me.type = type
        wasVisited = False
    End Sub
End Class

I have object of some other class named as "graph" where in constructor of graph class I am calling constructor of vertex class.

I have array of vertex class: Public vertices() As vertex

And redim vertices(2000): resizing array again for some reason.

Now, when i loop the array to check empty value it throws an error:

Object reference not set to an instance of an object. (Since value contains "nothing")

even though i am checking like this,

If (vertices(i).name) Is Nothing Then
            Exit For
        End If

How can i check empty element of array?

3 Answers 3

1

Since you seem to want your collection be dynamic, a List(Of vertex) would serve you better. that along with a default New() constructor and you can add, remove, sort, search, whatever you need. To check for any empty value you can use If Vertices(i).name = "" then

Public Class vertex
    Public wasVisited As Boolean
    Public name, type As String
    Public x_pos, y_pos As Double
    Public Sub New()
        wasVisited = False
        name = ""
        type = ""
        x_pos = 0
        y_pos = 0
    End Sub

    Public Sub New(ByVal x_pos As Double, ByVal y_pos As Double, ByVal name As String, ByVal type As String)
        Me.x_pos = x_pos
        Me.y_pos = y_pos
        Me.name = name
        Me.type = type
        wasVisited = False
    End Sub
End Class

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim Vertices As New List(Of vertex)
    For I = 0 To 99
        Vertices.Add(New vertex())
        Vertices(I).name = "Test" + I.ToString
    Next
End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

What's the size of vertices() before the redim operation ? If it's less than 2000, then the added elements will be Nothing right after the array enlargement, therefore when you try to access the name property of vertices(i) for values of i that go beyond the initial array size you're actually trying to dereference a null object reference.

You either need to check that vertices(i) IsNot Nothing before testing for the value of its properties or make sure every element of the array is assigned a new vertex object.

If vertices(i) Is Nothing OrElse vertices(i).name Is Nothing Then
    Exit For
End If

Here's a thread on vbforums about a similar problem: http://www.vbforums.com/showthread.php?546668-RESOLVED-Redim-array-of-objects

Comments

0

Have you tried:

If Not vertices Is Nothing AndAlso Not vertices(i) Is Nothing _
            AndAlso Not vertices(i).name Is Nothing Then

   Dim value as string= vertices(i).name

End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.