Element Returns null

Element Returns null

mohd.fadel
Enthusiast Enthusiast
1,240 Views
2 Replies
Message 1 of 3

Element Returns null

mohd.fadel
Enthusiast
Enthusiast

Hello,

 

I don't know what i'm missing here.. I'm trying to set the value of a parameter after i retrieve the element from a set of ids. However, the element is returning null and i'm a receiving an error "Object reference not set to an instance of an object"

 

Element elem = activeDoc.GetElement("224716") as Element;

Parameter RW_CV1 = elem.LookupParameter("RW_CV-1");
RW_CV1.Set("New");
RW_CV1.SetValueString("New");

 

please note that 224716 is a valid id of the element. I can double check that using select by id.

 

Any help is appreciated.

 

Mohamed.

0 Likes
Accepted solutions (1)
1,241 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni
Accepted solution

So, you are saying that 224716 is the valid integer value of the element id.

 

That is good to know.

 

Please note that the string "224716" is something completely different. A string is not a number, nor an element id.

 

In this case, however, a string might very well be a Revit unique id.

 

And, lo and behold, the GetElement method provides three overloads, one taking an element id, another a string-valued unique id:

 

https://www.revitapidocs.com/2021.1/8760be74-50fd-6809-aec6-3cb168d03ea5.htm

 

To make use of your integer-valued element id, you need to convert the number (not the string) to an ElementId object and pass in that to GetElement. Since this is demonstrated in a numerous different places, I am sure you have already solved this problem yourself long before I finished typing this.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

mohd.fadel
Enthusiast
Enthusiast

Thank you Jeremy. That did it.

0 Likes