Can’t connect to X11 window server using ‘:0’ as the value of the DISPLAY variable.

I’ve decided to try out Netbeans, as it had been recommended to me as a good IDE for PHP development, but while attempting to emerge it under Gentoo, I was greeted by this error:

Can't connect to X11 window server using ':0' as the
value of the DISPLAY variable.

The problem seemed to be that I was using sudo to run the ’emerge netbeans’ command – becoming root using su seems to solve the problem and let netbeans compile.