When erectile dysfunction is caused by psychological factors, the man can have spontaneous erections when sleeping or early in the morning. But he may not be able to achieve an erection when having sex.