Error trying to display array member

E

Eric A. Johnson

For the following code:

' return String representation of CTriangleShape

Public Overrides Function ToString() As String

' use MyBase reference to return CShape String

Return MyBase.ToString & ";" & vbCrLf & _

"Size of sides: " & mSideLength(0).ToString '& _

' ", " & mSideLength(1).ToString & ", " & _

' ", and " & mSideLength(2).ToString

End Function ' ToString



I get this error:



An unhandled exception of type 'System.NullReferenceException' occurred in
ShapeLibrary.exe

Additional information: Object reference not set to an instance of an
object.

.... I have tested it, and discovered that it is the mSideLength(0, 1, or 2)
sections that are causing the error, not any other portion. The problem is,
I wish to display each of the side lengths within the ToString function.
The mSideLength array is declared like so:

' Array containing the length of each side

Private mSideLength As Integer()



and instantiated thusly:



' constructor for scalene triangle

Public Sub New(ByVal side1 As Integer, _

ByVal side2 As Integer, ByVal side3 As Integer)

MyBase.New(mSides)

mSideLength = New Integer(mSides - 1) _

{side1, side2, side3}

Console.WriteLine("CTriangle constructor: {0}", Me)

End Sub ' New scalene triangle



....where mSides is the number of sides of the shape. Since it is a
triangle, this is of course 3 sides. Can anyone help me figure out why it
doesn't seem to want to display the side lengths? I have already, as you
can see, given it side lengths; if you can explain to me why it claims that
the object reference is not set to an instance of an object, when it seems
clear to me that mSideLength is clearly an instance of an Integer object (as
demonstrated by the New function), I would appreciate it greatly. I would
appreciate it even more if you can help me to understand how to fix this
problem! Thanks for your help.

-- Eric
 
E

Eric A. Johnson

I still need help... I was able to get a property made, thinking that might
help. Here is the property:

' Property for the length of a side

Public Property SideLength(ByVal sideNumber As Integer) As Integer

Get

Return mSideLength(sideNumber)

End Get

Set(ByVal Value As Integer)

mSideLength(sideNumber) = Value

End Set

End Property

End Class ' CTriangle

....and yet, I still obtain the same result: the Get portion (Return
mSideLength(sideNumber)) is highlighted in yellow, and it has the same
error -- "Object reference not set to an instance of an object." When I
let the mouse cursor hover over the "mSideLength" portion of the Get: Return
statement, it declares it as being "Nothing". This confuses me. Shouldn't
it be created as an Integer? The '0' part for the sideNumber is okay. It's
the array variable itself that appears to not be created, which is strange,
since it seems to be created within the constructor. Or should I
instantiate it when or right after I create it with "Private mSideLength As
Integer()"? Please help!

-- Eric
 
C

Cor Ligthert [MVP]

Eric,

Can you first past code in a notebook, cut it and than paste it in your
message. At least in OE is this at least for me very difficult to get an
idea from the code.

Thanks

Cor
 
E

Eric A. Johnson

Cor,

I would be happy to do so for you. First, allow me to explain the
latest on this problem. I have traced and discovered that, before it ever
instantiates the array within the constructor, it calls the MyBase.New()
constructor. This is for class CTwoDimensionalShape, which in turn also
calls MyBase.New(). This, then, is for class CShape. The CShape
constructor attempts to WriteLine Me (which, of course, calls
Me.ToString() ). Unfortunately, the "Me" that is being referred to is
actually triangle, the object of class CTriangle! Since VB is still running
the original "MyBase.New()" step, it has not yet instantiated the array.
Okay, so I decide to try instantiating the array within the declaration
at the beginning of the class, then assigning it the proper values within
the constructor, so that at least it has something to work with for the
ToString method. Only problem is, it still crashes. When I put a
breakpoint at the declaration, it never stops, seemingly indicating that
it's not even being declared (which is odd, since other members are declared
properly before the constructor -- or at least, they're able to be accessed,
whether directly or via property, within constructors normally). I am at my
wit's end with this problem. I will post the entire CTriangle class here;
if you need me to post the other classes for you, I am happy to do so.

-- Eric

' **********************************************************************
' * File : Triangle.vb *
' * Author : Eric A. Johnson *
' * Date : 09/10/05 *
' * Purpose: A derived class that defines a triangle. *
' **********************************************************************

Public Class CTriangle
Inherits CTwoDimensionalShape ' A triangle is a 2d shape

' All triangles have three sides
Private Const mSides As Integer = 3

' Array containing the length of each side
Private mSideLength As Integer() = _
New Integer() {1, 1, 1}


' default constructor
Public Sub New()
' default, implicit call to CShape constructor is okay
' because it automatically defines the shape with 3 sides

' define each side as having length 1
' use mSides - 1 due to starting at 0
' mSideLength = New Integer(mSides - 1) {1, 1, 1}

Console.WriteLine("CTriangle constructor: {0}", Me)
End Sub ' New

' constructor for equilateral triangle
Public Sub New(ByVal sideSize As Integer)
MyBase.New(mSides)
'mSideLength = New Integer(mSides - 1) _
'{sideSize, sideSize, sideSize}

Console.WriteLine("CTriangle constructor: {0}", Me)
End Sub ' New equilateral triangle

' constructor for isosceles triangle
Public Sub New(ByVal isoscelesSide As Integer, _
ByVal oddSide As Integer)

MyBase.New(mSides)
'mSideLength = New Integer(mSides - 1) _
'{isoscelesSide, isoscelesSide, oddSide}

Console.WriteLine("CTriangle constructor: {0}", Me)
End Sub ' New isosceles triangle

' constructor for scalene triangle
Public Sub New(ByVal side1 As Integer, _
ByVal side2 As Integer, ByVal side3 As Integer)
' ERROR: The next statement calls the New statement for
' CTwoDimensionalShape, which calls New for CShape. That,
' in turn, tries to WriteLine Me. I have finally figured
' out in Debug mode that the Me that is accessed is
' Triangle, *not* Shape, even though Shape is calling the
' Me reference. Since mSideLength is not yet instantiated,
' it invariably crashes here. Furthermore, I have discovered
' that I cannot change the location of MyBase.New; it has to be
' the first executable line.
' POSSIBLE SOLUTION: Instantiate the array only once, right after
' it is created; then simply change what it equals later inside
' the constructor. Will test this idea now.
MyBase.New(mSides)

SideLength(0) = side1
SideLength(1) = side2
SideLength(2) = side3

Console.WriteLine("CTriangle constructor: {0}", Me)
End Sub ' New scalene triangle

' finalizer overrides version in class CTriangle
Protected Overrides Sub Finalize()
Console.WriteLine("CTriangle finalizer: {0}", Me)
MyBase.Finalize()
End Sub ' Finalize

' return String representation of CTriangleShape
Public Overrides Function ToString() As String

' use MyBase reference to return CShape String
Return MyBase.ToString & ";" & vbCrLf & _
"Size of sides: " & SideLength(0).ToString '& _
' ", " & mSideLength(1).ToString & ", " & _
' ", and " & mSideLength(2).ToString
End Function ' ToString

' Property for the length of a side
Public Property SideLength(ByVal sideNumber As Integer) As Integer
Get
Return mSideLength(sideNumber)
End Get

Set(ByVal Value As Integer)
mSideLength(sideNumber) = Value
End Set
End Property
End Class ' CTriangle
 
C

Cor Ligthert [MVP]

Eric,

I think that you are in a to low level in your application that anybody can
help you from a newsgroup. Try to simulate the process by an for other
people as well in short time tracable routine. Sending a complete class with
all its ins and outs will probably give you no result at this free
newsgroup. Therefore you need consultancy.

However, the behaviour you tell in the debugger sounds a lot as recursion. A
method is accessed more than once in a loop calling himself. 'Me' by the way
is always the current class. (what it can be in that recursion).

Have a look for that, or create a small simulation program.

I hope this helps anyhow,

Cor
 

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