Object array throwing an access error

J

John Dann

I'm getting an NullReferenceException that I don't understand when
trying to access an array of objects within another object. In
simplified terms the code I'm trying to use is as follows:

Public Class MyInnerClass
Public Name as String
' And other public properties
End Class

Public Class MyMainClass
Dim m_InnerObjects() as MyInnerClass

Friend Property InnerObjs(ByVal i as Integer) as MyInnerClass
Get
Return m_InnerObjects(i)
End Get
Set (ByVal value as MyInnerClass)
m_InnerObjects(i) = value
End Set
End Property
End Class

'Within main module
Dim oMMC as New MyMainClass
oMMC.InnerObjects(some-integer).Name = some-string

It's this last line that throws the NullReferenceException. The above
is just a skeleton of the code - I've omitted a lot of detail like
dimensioning the array of MyInnerClass.

But can anyone spot if there's anything fundamental wrong with what
I'm trying to do here? Or is the problem maybe in the extra detail
that I haven't given?

Maybe there's a better way of managing the collection of MyInnerClass
objects than using an array?

Many thanks
JGD
 
R

rowe_newsgroups

I'm getting an NullReferenceException that I don't understand when
trying to access an array of objects within another object. In
simplified terms the code I'm trying to use is as follows:

Public Class MyInnerClass
    Public Name as String
    ' And other public properties
End Class

Public Class MyMainClass
    Dim m_InnerObjects() as MyInnerClass

    Friend Property InnerObjs(ByVal i as Integer) as MyInnerClass
        Get
            Return m_InnerObjects(i)
        End Get
        Set (ByVal value as MyInnerClass)
            m_InnerObjects(i) = value
        End Set
    End Property
End Class

'Within main module
Dim oMMC as New MyMainClass
oMMC.InnerObjects(some-integer).Name = some-string

It's this last line that throws the NullReferenceException. The above
is just a skeleton of the code - I've omitted a lot of detail like
dimensioning the array of MyInnerClass.

But can anyone spot if there's anything fundamental wrong with what
I'm trying to do here? Or is the problem maybe in the extra detail
that I haven't given?

Maybe there's a better way of managing the collection of MyInnerClass
objects than using an array?

Many thanks
JGD

Are you sure you are actually instantiating the array? You've omitted
that code, but I'm thinking that it might not be running. Also, when
you run the app in debug mode, which variable is is saying is null?
The array or the item you are trying to find in the array?

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
A

Armin Zingler

John said:
I'm getting an NullReferenceException that I don't understand when
trying to access an array of objects within another object. In
simplified terms the code I'm trying to use is as follows:

Public Class MyInnerClass
Public Name as String
' And other public properties
End Class

Public Class MyMainClass
Dim m_InnerObjects() as MyInnerClass

Friend Property InnerObjs(ByVal i as Integer) as MyInnerClass
Get
Return m_InnerObjects(i)
End Get
Set (ByVal value as MyInnerClass)
m_InnerObjects(i) = value
End Set
End Property
End Class

'Within main module
Dim oMMC as New MyMainClass
oMMC.InnerObjects(some-integer).Name = some-string

It's this last line that throws the NullReferenceException. The above
is just a skeleton of the code - I've omitted a lot of detail like
dimensioning the array of MyInnerClass.

Dimensioning is the decisive part in this case, so it's hard to find the
problem.
But can anyone spot if there's anything fundamental wrong with what
I'm trying to do here? Or is the problem maybe in the extra detail
that I haven't given?

Yep. ;-)
Maybe there's a better way of managing the collection of MyInnerClass
objects than using an array?

I'd create the Array in the class' constructor if the array has to be there
as long as the object lives. Using a List(Of) is an alternative (better then
redim preserve) but wouldn't solve the problem here.


Armin
 
J

John Dann

Ok, many thanks for the pointers. I've now realised that I need to
instantiate each and every object element in the array with a
For...Next loop inside a ReDim sub, which I wasn't doing previously
and all is now OK. My InnerClass was originally a Structure in an
earlier version of this code and didn't need instantiating in the same
way, which is why it got overlooked when I converted it to a class.

JGD
 
R

rowe_newsgroups

Ok, many thanks for the pointers. I've now realised that I need to
instantiate each and every object element in the array with a
For...Next loop inside a ReDim sub, which I wasn't doing previously
and all is now OK. My InnerClass was originally a Structure in an
earlier version of this code and didn't need instantiating in the same
way, which is why it got overlooked when I converted it to a class.

JGD

If you are tReDim'ing the array constantly, I would urge you to
convert the array into a List(Of T) instead, and where ever you need
an array you can simple just call .ToArray() on the list.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top