fix accessing invalid subcategory iterators

Moving before the first or behind the last sub category triggers a segfault caused by an invalid iterator.