diff --git a/weblinks.module b/weblinks.module
index 64e0eb9..e9f27c9 100644
--- a/weblinks.module
+++ b/weblinks.module
@@ -1051,15 +1051,18 @@ function weblinks_get_tree($parent = 0, $quick = FALSE, $ismainlinkspage = TRUE)
 
   // Is this a top level request?
   if ($parent) {
-    // The existing elements have depths one too low.
     foreach ($tree as $term) {
-      // Increase the depth of each term by 1, so that we can insert the
-      // requested parent at depth 0 afterwards.
+      // Increase the depth of each existing term by 1, so that we can insert
+      // the requested parent at depth 0.
       ++$term->depth;
     }
-    // Not top level, so we need to get the requested term
-    // and stick it on the front of the tree.
+    // Get the requested term and put it at the front of the tree.
     $parent_term = taxonomy_term_load($parent);
+    if (!$parent_term) {
+      // The numeric $parent id is not a valid group, so give error and exit.
+      drupal_set_message(t('Invalid group requested (%tid).', array('%tid' => $parent)), 'error');
+      return array();
+    }
     // Put parent term at top of array and set depth to 0.
     array_unshift($tree, $parent_term);
     $tree[0]->depth = 0;
