Hi, I think the problem is that the root node does not have a object reference assigned to it. If you check in the debugger, you will see that root_node.ObjectRef is always null. Also in your second attempt it is a similar problem. Here you are trying to get the CustAttribConjatiner from the root node (but not the object referenced by the node). In this case, the container is also probably null, so the request to get the first attribute will cause the crash.
The crash is occuring because you are technically trying to access a NULL pointer. In C++ this is not good and is why C++ code is usually full of checks for NULL. Because the managed .NET wrappers are simply wrapping the C++ code, the problem exists there too, and is why you also get the crash. A better formed managed API would prevent this... but it is, what it is. 🙂
So, to fix this in your code, first you should get into the habit of using the managed API like a C++ programmer and check for null. For example, you can prevent the crash around the referenced object by doing this:
IObject obj = ip.RootNode.ObjectRef;
if (obj == null)
Debug.Print("There is no object referenced by this node."); // do something to prevent calling the null pointer
Next, note that the custom attribute container can exist on any animatible derived object. This can include the node or object referenced by the node. In your second example, you are approaching it this way. But, the Custom Attribute Container is not always allocated either. Normally it is only allocated when there is attributes present. So again, you are assuming you have a container, and probably in your test the container does not exist and the property is returning null. A better way to handle it would be like this:
IICustAttribContainer cont = ip.RootNode.CustAttribContainer;
if (cont == null) // do something to prevent a call being made on a null object.
{
ip.RootNode.AllocCustAttribContainer(); // here we can allocate it...
cont = ip.RootNode.CustAttribContainer;
if (cont == null) // double check to make sure the allocation worked well...
return; // something is not good, so exit routine...
}
// now we can access the container safely...
ICustAttrib att = cont.GetCustAttrib(0);
// in this logic, this will always be null because we just allocated a new container. But here you could also go ahead and add one, too.
if (att != null)
{
Debug.Print("Custom Attribute Name = " + att.Name);
}
else
Debug.Print("No Custom Attributes found; Probably had a new container allocated. :-)");
Hope it helps.
Kevin
Kevin Vandecar
Developer Technical Services
Autodesk Developer Network