System.NullReferenceException: Object reference not set to an instance of an obj

System.NullReferenceException: Object reference not set to an instance of an obj

Anonymous
Not applicable
3,457 Views
4 Replies
Message 1 of 5

System.NullReferenceException: Object reference not set to an instance of an obj

Anonymous
Not applicable

Hi!

I have the following lines of C# code: 

foreach (ObjectId attID in titleBlock.AttributeCollection)
{
AttributeReference attRef = (AttributeReference)attID.GetObject(OpenMode.ForRead);

if (String.Equals(attRef.Tag, UserPreferences.Instance.ABC, StringComparison.OrdinalIgnoreCase))
{
vendorIP = false;
}

...

I am trying to figure out why GetObject keeps throwing me the exception "System.NullReferenceException: Object reference not set to an instance of an obj." attID is not Null. The documentation for GetObject indicates that it is looking for and object with the objectID of attID in attID. That doesn't make sense to me. For the code I really only need attRef.Tag which is a string. I could circumvent the this exception by just getting the Tag from attID. I'm confused and would like to ask for some guidance on how to eliminate this exception. BTW I did not write this code myself and my C# skills are just intermediate. Thanks in advance! 

0 Likes
Accepted solutions (1)
3,458 Views
4 Replies
Replies (4)
Message 2 of 5

Alexander.Rivilis
Mentor
Mentor

@Anonymous 

Transaction was started?

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 3 of 5

moogalm
Autodesk Support
Autodesk Support
Accepted solution

1) You can check ObjectId.IsValid if this API property return true

2) The recommended approach to access the object from its object id is use transaction, you can get basic understanding of this from here

PseudoCode:

using(OpenCloseTransaction o = new OpenCloseTransaction) {
  foreach(ObjectId attID in titleBlock.AttributeCollection) {

   //AttributeReference attRef = (AttributeReference)attID.GetObject(OpenMode.ForRead);

   AttributeReference attRef = o.GetObject(attID, OpenMode.ForRead) as AttributeReference;

   if (String.Equals(attRef.Tag, UserPreferences.Instance.ABC, StringComparison.OrdinalIgnoreCase)) {
    vendorIP = false;
   }
  }
Message 4 of 5

Anonymous
Not applicable

Thank you very much! This worked and the code makes a lot more sense!

0 Likes
Message 5 of 5

Anonymous
Not applicable

NullReferenceException means that you are trying to use a reference variable whose value is Nothing/null . When the value is Nothing/null for the reference variable, which means that it is not actually holding a reference to an instance of any object that exists on the heap.

string str = null;
str.ToUpper();

 

Above c# code throws a NullReferenceException at the second line because you can't call the instance method ToUpper() on a string reference pointing to null. So, check your code once again.

 

0 Likes