473,499 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error trying to display array member

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


Nov 21 '05 #1
4 1455
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

"Eric A. Johnson" <no*****@dontlookforme.com> wrote in message
news:NY*************@newssvr27.news.prodigy.net...
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

Nov 21 '05 #2
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
Nov 21 '05 #3
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
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #4
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
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1850
by: Oeln | last post by:
If I want to check for input of an integer I've got the following (I get the form input with $input = "$_POST"): if(!ereg("^+$",$_POST)) { echo "Input is incomplete or incorrect."; } If,...
2
4683
by: Dica | last post by:
i've got a table in my dataSet that looks like this: <tblTasks> <taskID>0</taskID> <projectID>0</projectID> <summary>-- Select A Task --</summary>
6
3539
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
2
2074
by: KiranJyothi | last post by:
Can anyone please tell me how can I rectify my compile time error in this program. /********************************HEADER FILE***********************************/ #ifndef POLYNOMIAL_H //...
25
4347
by: Blasting Cap | last post by:
I keep getting errors that pop up when I am trying to convert an application from dotnet framework 1.1 to framework 2.0. The old project was saved in sourcesafe from Visual Studio 2003, and I have...
1
2792
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
4
3834
by: Ty | last post by:
Hello all, I am creating a web site with Visual Stuido 2008. I am trying to use a java script file to create a busybox for login from this page http://blogs.crsw.com/mark/articles/642.aspx. I...
2
31734
by: tridirk | last post by:
Hi; I am getting a Objceted Expected Error on my forum site. I can't find what is wrong? Line: Char: Error: Object expected Code:0 the site is My SMF Forum
3
1497
by: alexdavies82 | last post by:
I am having a problem producing a relatively simple PHP code. Whenever I run it it gives the following fault message:
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.