I am working on a development where I should invoke my testcases through the Method Reflect API. Currently, I am able to invoke the method and I am also able to get the output after invoking a specific method. But after the method invocation, the invoke method is throwing NullPointerException.
I have tried to add a try and catch so that, I can know exactly which part of my code is throwing the NullException. Based on my observation this code is the culprit here. passed = (boolean) payload.invoke(testcase);
Here's my code surrounded with try and catch:
public void execute() {
try {
Matcher m = Pattern.compile("([^()]*)[(]([^()]*)[)]").matcher(getPayloadString());
m.find();
boolean passed;
if (m.group(2).isEmpty()) {
System.out.println("m.group(2) is empty!!!!\n"); //This line of code is being executed. Because my argument is empty.
passed = (boolean) payload.invoke(testcase);
System.out.println("Boolean is: " + passed + "\n");
}else {
System.out.println("Inside else!!!\n");
passed = (boolean) payload.invoke(testcase, ObjectCreator.convertParameters(m.group(2)));
System.out.println("Boolean2 is: " + passed + "\n");
}
//If already invoked the method, this line should be executed. But now it didnt print out this debug message below.
System.out.println("Already exited the convertParameters()\n");
String output = passed ? "PASS" : "FAIL";
TestCase.printResult(testcase.getClass().getName(), output, "", "", "");
} catch (Exception ex) {
System.out.println("Exception in execute is : " + ex.toString() + "\n");
}
}
I am not really sure why is it throwing NULLPointerException.