vendredi 27 février 2015

Array not equal to null when assigned null

arguments is not equal to null. Or there might be something else that I am missing.


The purpose of the code is to take any given String and handle the three different cases.



  1. "doSomething" => invoke doSomething()

  2. "doSomethingElse,2" => invoke doSomething(2)

  3. "doSomethingElsePlease,3,4" => invoke doSomething(3,4)


Number 2 and 3 can both be handled with varargs and passing the arguments as an array. Number 1 is were the problem occurs. The error that occurs is that


h("doSomething"); is trying to call for doSomething(null) {...}



private void h (String abc) {
String method = (abc.indexOf(",") == -1) ? abc: abc.substring(0, abc.indexOf(","));
System.out.println("Method: |" + method+"|");
String[] arguments = (abc.indexOf(",") == -1) ? null : abc.substring(abc.indexOf(",")).split(",");
System.out.println("Arguments: " + Arrays.toString(arguments));

try {
if (arguments == null) {
this.getClass().getMethod(method, String.class).invoke(this);
} else {
System.exit(0);
this.getClass().getMethod(method, String.class).invoke(this, Arrays.asList(arguments));
}
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException
| SecurityException e) {
e.printStackTrace();
}
}





Aucun commentaire:

Enregistrer un commentaire