Prevent restoring focus to last element if none exist

Fixed a null pointer exception when trying to restore focus to the last element where that element doesn't exist.