Change from throw new NullPointerException to throw new IllegalArgumentException because NullPointerException is throwed when accessing a null object and the null value is not accepted. Thanks to Ruben for the comment.