The dreaded - Object reference not set to an instance of an object

The dreaded - Object reference not set to an instance of an object

darren.lees
Contributor Contributor
1,537 Views
5 Replies
Message 1 of 6

The dreaded - Object reference not set to an instance of an object

darren.lees
Contributor
Contributor

Hi All,

 

I have another, hopefully, eaay problem.

 

I wish to interrogate a parts list on a drawing.  Something I've done in the past.

 

However, this time I'm tring to populate an array with each of the values in the REVISION column.

I'll then figure out how to find the highest value character in the array and do some magic in the drawing based on that. 

 

My dilema is, when I try to add a value to the array inside a FOR LOOP, I get the dreaded "Object reference not set to an instance of an object" error and my limited knowledge of VB.NET can't sem to get past it.  It has to be an easy fix.

 

Code below.....

 

' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oPartList As PartsList
oPartList = oDrawDoc.ActiveSheet.PartsLists.Item(1)

' Iterate through the contents of the parts list
Dim i As Long
Dim LetterArray() As String
For i = 1 To oPartList.PartsListRows.Count
	'look at only the REVISION column
	oCell = oPartList.PartsListRows.Item(i).Item("REVISION")
	
	'Test to see if we have the correct REVISION letter
	MessageBox.Show("Revision of item " & i & " is " & oCell.Value, "Revs")

	'Populate the array with the REVISION letter
	LetterArray(i) = oCell.Value
	'PRODUCES THIS - Object reference not set to an instance of an object
	'PLEASE HELP!!!   I don't ask for much.  I'm a good man.....really!!!
	'I pay my bills and taxes......etc!!
Next
	

 

 

Please help!!

This was easier when we just used BASIC as a language.  Who thought it was a good idea to invent different computer languages??  Sheesh!!! 🤣

 

 

 

0 Likes
Accepted solutions (2)
1,538 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @darren.lees.  When you declare an Array type variable, it can be tricky.  You either have to not specify the size/length of the array, then set all of its contents at that line of code, or you must specify its size/length, then not specify its contents at that line of code, then fill in its contents later, being careful not to go pas that pre-set size.  It is often easier to create a 'New List(Of String)' when you do not know how many entries you may need to put into it.  Then just use its Add or AddRange method to add its contents.  Its size/length does not matter, plus a List is super easy to 'Sort' later.  If you need to specify size, you may be able to use 'oPartList.PartsListRows.Count - 1', because Arrays are zero based (zero is its first entry index).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

dleesuk
Advocate
Advocate

Hi @WCrihfield,

 

We meet again.

 

I'll give your suggestion a go tomorrow and see what happens.

 

Thanks for now.


Regards

Darren
0 Likes
Message 4 of 6

Frederick_Law
Mentor
Mentor

Or, when the String is declared, it is "null".

A zero length String.

Hence LetterArray(1) fail.

Try LetterArray = LetterArray + oCell.Value.

0 Likes
Message 5 of 6

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @dleesuk 

 

You can use something like this to find the largest rev letter.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis

 

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oPartList As PartsList
oPartList = oDrawDoc.ActiveSheet.PartsLists.Item(1)

oMax = "A"
oRowMax = 1

' Iterate through the contents of the parts list
Dim i As Long
For i = 1 To oPartList.PartsListRows.Count
	'look at only the REVISION column
	oCell = oPartList.PartsListRows.Item(i).Item("REVISION")

	Dim letterarray As New ArrayList
	letterarray.Add(oCell.Value)
	letterarray.Add(oMax)
	letterarray.Sort
	oMax = letterarray.Item(1)

	If oCell.Value = oMax Then oRowMax = i
Next

MsgBox("Row " & oRowMax & " has the largest rev letter of " & _
oPartList.PartsListRows.Item(oRowMax).Item("REVISION").Value, , "iLogic")

 

EESignature

Message 6 of 6

dleesuk
Advocate
Advocate

Hi Curtis,

 

Thank you for that.  It works very well!!   A big shout out to @WCrihfield & @Frederick_Law also.

 

Now onto the 'magic' I spoke of earlier.


Regards

Darren
0 Likes