java - How to check to see that a set of variables is not null before continuing -


i have class extends org.apache.ant.tools.task. class has 5 variables set via public setters:

private string server; private string username; private string password; private string appname; private string version; private string file; 

and there public execute() method invoked ant:

public void execute() throws buildexception {     checkargs()     ... // execute code goes here } 

before execute runs, want check none of required variables null and, if so, throw buildexception() describing problem, user in ant has idea what's wrong:

private void checkargs() {     if (server == null) {         throw new buildexception("server cannot null.");     }      if (username == null) {         throw new buildexception("username cannot null.");     }      if (password == null) {         throw new buildexception("password cannot null.");     }      if (file == null) {         throw new buildexception("file cannot null.");     }      if (version == null) {         throw new buildexception("version cannot null.");     } } 

is there less verbose way this? hate repeated use of if , if there's more efficient way it, i'd love see it. can imagine how if had, say, 20 different variables need check before execute() can run.

what method validating large numbers of different variables precursor continuing code execution or throwing useful error message?

you store args in hashmap<string, string> argmap, mapping argument names values. adjust getters/setters accordingly. then:

for (string key : argmap.keyset()) {     if (argmap.get(key) == null) {         throw new buildexception(key + " cannot null.");     } } 

Comments