×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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.

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

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 --> vb.net_pseudocode

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 --> vb.net_pseudocode

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

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

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources