# Crash when creating child nodes from and loading program - Custom API

**URL:** <https://forum.universal-robots.com/t/crash-when-creating-child-nodes-from-and-loading-program-custom-api/3463>\
**Category:** URCap Development\
**Tags:** welding\
**Created:** [January 15, 2019, 8:29pm UTC](https://forum.universal-robots.com/t/crash-when-creating-child-nodes-from-and-loading-program-custom-api/3463 "2019-01-15T20:29:17Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![dmartin](https://avatars.discourse-cdn.com/v4/letter/d/c4cdca/32.png) [@dmartin](https://forum.universal-robots.com/u/dmartin)\
**Post date:** [June 25, 2019, 12:52pm UTC](https://forum.universal-robots.com/t/crash-when-creating-child-nodes-from-and-loading-program-custom-api/3463/10 "2019-06-25T12:52:44Z")

</div>

Hi @ajp! Hi @mmi! Hi @anon63360713!

Thank you all for your help. I was fighting with similar problems. So I put here my summary in case it help any of you.

I was given this two examples by the UR R&D Department:

```
  TreeNode tree = this.programAPI.getProgramModel().getRootTreeNode(this);
  final GripperMarkerInterface[] gripperContributionInterface = {null};
      tree.traverse(new ProgramNodeVisitor() {
        @Override
        public void visit(URCapProgramNode programNode, int index, int depth) {
          if(programNode.canGetAs(GripperMarkerInterface.class)) {
            GripperMarkerInterface urCapProgramNode = programNode.getAs(GripperMarkerInterface.class);
            gripperContributionInterface[0] = urCapProgramNode;
            System.err.println("ProgramNodeVisitor.visit: " + urCapProgramNode + ", title " + ((GripperOpenProgramNodeContribution) urCapProgramNode).getTitle());
          }
        }
      });
      tree.traverse(new URCapProgramNodeInterfaceVisitor<GripperMarkerInterface>() {
        @Override
        public void visitURCapAs(GripperMarkerInterface urCapProgramNode, int index, int depth) {
          gripperContributionInterface[0] = urCapProgramNode;
          System.err.println("URCapProgramNodeInterfaceVisitor.visitURCapAs: " + urCapProgramNode + ", title " + ((GripperOpenProgramNodeContribution)urCapProgramNode).getTitle());
        }
      });
      return gripperContributionInterface[0];

```

I ended up using the first approach, just because I was in a hurry (and involved in several different projects). So as the first was working to perfection, I didn’t test the second option.

Regarding the overall problem, and trying to summarize and close things.  
One of the difficulties was that I was having up to three different causes of problems, two of them closely related. This made even more difficult to know clearly what was the problem. The problems, solutions and comments are:

- **The need to _get root TreeNode every time_ I need to use it.**  
_ **Problem** _: I was getting my base root TreeNode just once in the class constructor. I thought it was a ‘static pointer’ all the time the program model is ‘alive’. I expected getting it just this once, I would see the children even if they were added afterwards.  
_ **Solution** _: Getting my root TreeNode **every time** I need it, using this three code lines:

- **The need to _manually manage initial children creation_, to create them just when the URCap node is new.**  
_ **Problem:** _ When a previously saved .urp is loaded, containing some of my URCap nodes, I was getting a variety of exceptions, mainly NullPointer exeptions, when trying to modify the inner structure of URCap. The nodes where shown in the program tree correctly, as they were saved. But trying to add/delete children nodes was always generation an exception.  
I quote an information from UR R&D Department: “When program is being loaded your constructor is called, and after it’s done all children are removed, and replaced with nodes loaded from urp file.” Well, I just confirmed this mechanism is not fully working, at least in my SDK simulator SW3.6.0.30512.  
_ **Solution attempts** _: While confirming if this is an API bug or not, and which versions are fixed, I tried a couple of ways:

- **Right usage of traverse, and how to use it with URCap nodes.**  
_ **Problem:** _ I couldn’t find enough examples for traverse usage, and none for using it for URCap nodes.  
_ **Solution:** _ Using your example code now I know how to use traverse. I also found this customAPI example that helped with the interface creation:

I’ve been polishing the code and I’m still running final tests. But so forth, it is working to perfection. So I hope it can help you too.  
And UR crew, please keep us up to date with your findings or solutions in this matter.

Thank you all!  
David Martin

---

_[View the full topic](https://forum.universal-robots.com/t/crash-when-creating-child-nodes-from-and-loading-program-custom-api/3463)._
