Your $user object doesn't have an ID yet.
Ensure that you have the $user saved to the database, so that you can safely get it's ID.