VB/NX System.NullReferenceException Object reference not set to an instance
VB/NX System.NullReferenceException Object reference not set to an instance
(OP)
Hello,
Has anyone received the System.NullReferenceException: Object reference not set to an instance of an object at NXJournal.Main() error message when attempting to perform an array operation?
I've declared a counter and an array number variable with the following:
Dim yCoordIndexCounter As Integer = 0
Dim sortedArrayOfYCoords() As Double
Then later in the code, I've used the counter within the array and set it equal to the y-coordinate in order to sort the y-coordinates later. But instead, arrayOfYCoords(yCoordIndexCounter) = origin1.Y is the line where I receive the above System.NullReferenceException error message.
I've research some of the reasons for my code generating this error message. And all I can figure out is that the array number variable is initially an empty set. Subsequently, how can you add to it or populate it if this message prevents numbers being added?
Thank you for your help ahead of time.
Has anyone received the System.NullReferenceException: Object reference not set to an instance of an object at NXJournal.Main() error message when attempting to perform an array operation?
I've declared a counter and an array number variable with the following:
Dim yCoordIndexCounter As Integer = 0
Dim sortedArrayOfYCoords() As Double
Then later in the code, I've used the counter within the array and set it equal to the y-coordinate in order to sort the y-coordinates later. But instead, arrayOfYCoords(yCoordIndexCounter) = origin1.Y is the line where I receive the above System.NullReferenceException error message.
I've research some of the reasons for my code generating this error message. And all I can figure out is that the array number variable is initially an empty set. Subsequently, how can you add to it or populate it if this message prevents numbers being added?
Thank you for your help ahead of time.





RE: VB/NX System.NullReferenceException Object reference not set to an instance
Array example:
CODE --> vb.net_pseudocode
As you can see, you have to manage your own bookkeeping with arrays. However, the .NET framework provides some classes to make your life easier. Instead of an array, I suggest using a List object. It has the advantage of handling the resizing issues for you behind the scenes.
List example:
CODE --> vb.net_pseudocode
You no longer need to increment a counter variable or resize the array. If you need to know how many items are in the list, you can use the .Count property.
MSDN array reference
MSDN ReDim reference
MSDN List reference
www.nxjournaling.com
RE: VB/NX System.NullReferenceException Object reference not set to an instance
The examples of the Array and List commands where spot on! So far, I tested my code with your suggested modifications of the "ReDim Preserve myValues(counter)," and it ran perfectly with no errors. I looked further into possibly using the List function, which at first appeared not to do one thing I was hoping for - sorting and reversing the sorting of the List. But then I reviewed the excellent links you provided, which shows that it is possible as well. I'll have to try that next.
I cannot thank you enough! You're awesome!!!
Sincerely,
NXProf