In your first attempt you are creating tree "myTree", and rest of your code refers to "tree1". This is incorrect way to do things since tree name has nothing to do with variable name which holds that tree. Your second attempt also does same wrong thing: "myTree" holds the tree, but code references "tree". Correct this, and then your second will work as expected.