Of course, your code does not do this, but what if your code looks like this:
public Server(int port) { new Thread(this, "Server").start(); try { //Opens a new server server = new ServerSocket(port); } catch (IOException ioe){ ioe.printStackTrace(); } } @Override public void run(){ if(server == null)throw new NullPointerException();// this may happen } }
The server link may be null, even if an exception does not occur. This is because Thread will use the created runnable and call the run method, even if the constructor of your class has not finished yet.
source share