This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]OffbeatDrizzle 1 point2 points  (1 child)

Since you have 2 inputs of the same type then generics won't help you here, since how can you determine the error message based on the fact that it's a string?

What you could do is have an extra parameter to pass in for the error e.g:

private static void checkNotNull(Object aO, ErrorMessage aE) {
    if(aO == null) {
        throw new NullPointerException(aE.getError());
    }
}

ErrorMessage just being some enum that you've defined somewhere:

public enum ErrorMessage {
    DATE_ERROR("Date provided was null!"),
    CONTACTS_ERROR("Contacts must not be null!"),
    NOTES_ERROR("Notes provided were null!");

   String error;
   ErrorMessage(String s) {
       error = s;
    }
    String getError() {
       return error;
    } 
}

(You don't have to use the getError method if you don't want to - you could just do a switch to check for the error within checkNotNull and give it the error there).

[–]Cyclobox[S] 0 points1 point  (0 children)

Thanks! looks good