How to fix "Undefined index"
With the following line of your code:

$username = $user_profile['field_username'];


It always gets executed and check index field_username. So in case if the index doesn't exist it throws error. 

If you replace your code with the following

if(empty($user_profile['field_username'])) {
  echo 'Please enter an user';
} else {
  print render ($user_profile['field_username']);
}


It will not always check field_username index but only if it is not empty.