The commonest cause of erectile dysfunction in young men is performance anxiety. Or in other words you are probably watching yourself trying to get an erection. Once you are up in the audience you are in the wrong place and it will never work. Just take intercourse off the menu for a while and you will be fine.
Several questions come to mind from your post. Do you usually wake up with morning erections or not? Is your penis not becoming hard during masturbation, with a sex partner, or during both? How is your overall general health? Do you smoke, get proper amount of sleep, suffer from excessive stress?