Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB/NX System.NullReferenceException Object reference not set to an instance 1

Status
Not open for further replies.

NXProf

Mechanical
Nov 28, 2012
45
0
0
US
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.
 
Replies continue below

Recommended for you

You have specified an array name, but not given it a dimension yet. To resize an array, you use the ReDim keyword; to keep the existing values intact, also use the Preserve keyword.

Array example:
Code:
Dim myValues() as Double
Dim counter as integer = 0

For each item in bigList
  ReDim Preserve myValues(counter)
  myValues(counter) = item.Value
  counter += 1
Next

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:
Imports System.Collections.Generic

Dim myValues as New List(Of Double)

For each item in bigList
  myValues.Add(item.Value)
Next

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
 
Thank you so very much cowski!!!

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
 
Status
Not open for further replies.
Back
Top