0

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.

Raaj Lokanathan
  • 479
  • 3
  • 7
  • 19
  • 1
    With `ex.printStackTrace();` you'll get also the line number which is helpful. In this case you should have done `if (m.find()) { ... now m.group(2) is not null ... }` – Joop Eggen Sep 07 '20 at 08:58
  • @JoopEggen I have added the printStackTrace and it points to the exact line which i mentioned in my question. Btw, I can't really understand by the f(m.find). The m.group(2) is always null because I dont have any arguments to pass for my methods. – Raaj Lokanathan Sep 07 '20 at 09:04
  • @matt Because once I call the method.invoke(testcase), it will execute the specific method and print out the result. Since I am able to view the result, I believe the method is being invoke. But after the invocation only it throws null. – Raaj Lokanathan Sep 07 '20 at 09:09
  • So it sounds like testcase and payload are not null. Then you have the other two options. testcase.payload is running and throwing an NPE after it prints out the result, or it is returning null. `(boolean)null` will cause an NPE. Is payload a `void` method? – matt Sep 07 '20 at 09:13
  • @matt as you mentioned I just checked both the payload and testcase is it null. It is not null. I guess the the testcase.payload is the one throwing NPE. The payload is a void method. So there isnt chances for it to return null. How can I fix this issue if the test.payload is returning NPE after the result? – Raaj Lokanathan Sep 07 '20 at 09:17
  • If not found `m.find()` returns false, (or else the group is not matched when optional like in `"...(...)*..."`),, and in both cases `m.group(2)` will be `null`. `m.group(2).isEmpty()` will throw a NPE. – Joop Eggen Sep 07 '20 at 09:27
  • My guess: `getPayloadString()` does not contain `"(...)"` - maybe `%28...%29` ? – Joop Eggen Sep 07 '20 at 09:32
  • "The payload is a void method." how about you check what is returned before casting it to boolean. Or read the [Method.invoke](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Method.html#invoke(java.lang.Object,java.lang.Object...)) javadoc. "If the underlying method return type is void, the invocation returns null." – matt Sep 07 '20 at 10:52

0 Answers0