Assuming you get this exception when this line is executed:
treeData = treeView.dataSource.view();
this means you don't have a proper reference to the TreeView. This could be due to passing an incorrect id value here:
treeView = $(
so we can suggest checking whether an element with this id exist by the time you are trying to access it: $("#" + treeId)
If you post a runnable dojo example, which demonstrates the issue we would be able to inspect your code and check why the treeView variable does not hold a valid reference to the widget.