groovyjmeterperformance-testingjmeter-pluginsjmeter-5.0

Getting Null Pointer in Method Invocation while getting Variable data


In a Jmeter script, I have collected some data in a 2 variables using JSON Extractor using Match No as -1 in both JSON extractor. Now I need to check in a API whether 2nd variable consists data of 1st variable or not using JSR223 Assertion. The code I am using is:

// Get the value of dependentFeatID variable from JMeter variables
String dependentFeatID = vars.get("dependentFeatID")

// Check if dependentFeatID is not null
if (dependentFeatID != null) {
    // Split the dependentFeatID into an array of IDs
    String[] dependentFeatIDs = dependentFeatID.split(",")

    // Check if dependentFeatIDs array is not empty
    if (dependentFeatIDs.length > 0) {
        // Get the first ID from dependentFeatIDs array
        String searchString = dependentFeatIDs[0]

        log.info("searchString: $searchString")

        log.info(dependentFeatIDs[1]+"AND "+dependentFeatIDs[2])

        // Get the value of featureIDs variable from JMeter variables
        String featureIDs = vars.get("featureIDs")

        // Check if featureIDs is not null
        if (featureIDs != null) {
            // Split the featureIDs into an array of IDs
            String[] ids = featureIDs.split(",")

            log.info(ids[0]+"AND "+ids[1]+"AND "+ids[2]+"AND "+ids[3])

            
            boolean idFound = false

            // Iterate over each ID in the array
            for (String id : ids) {
                // Trim any leading or trailing whitespace from the ID
                id = id.trim()
                // Check if the current ID matches the search string
                if (id.equals(searchString)) {
                    // If the ID is found, set idFound to true and break out of the loop
                    idFound = true
                    break
                }
            }

            // Set the assertion result based on whether the ID is found
            AssertionResult.setFailure(!idFound)
            if (!idFound) {
                AssertionResult.setFailureMessage("ID not found in var_id: $searchString")
            }
        } else {
            // Log an error if featureIDs is null
            log.error("featureIDs variable is null")
            AssertionResult.setFailure(true)
            AssertionResult.setFailureMessage("featureIDs variable is null")
        }
    } else {
        // Log an error if dependentFeatIDs array is empty
        log.error("dependentFeatIDs array is empty")
        AssertionResult.setFailure(true)
        AssertionResult.setFailureMessage("dependentFeatIDs array is empty")
    }
} else {
    // Log an error if dependentFeatID is null
    log.error("dependentFeatID variable is null")
    AssertionResult.setFailure(true)
    AssertionResult.setFailureMessage("dependentFeatID variable is null")
}

But I am getting Null Pointer in Method Invocation error while executing. While the JSON path I have used to extract data is tested and I am getting values as well for both variables. Please help me here. Here is detailed logs from jmeter console:

org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: String dependentFeatID = ""; dependentFeatID = vars.get("dependentFeatID"); St . . . '' : Typed variable declaration at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:179) ~[ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.util.BeanShellInterpreter.eval(BeanShellInterpreter.java:185) ~[ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.util.BeanShellTestElement.processFileOrScript(BeanShellTestElement.java:174) ~[ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.util.BeanShellTestElement.processFileOrScript(BeanShellTestElement.java:140) ~[ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.assertions.BeanShellAssertion.getResult(BeanShellAssertion.java:106) [ApacheJMeter_components.jar:5.6.3] at org.apache.jmeter.threads.JMeterThread.processAssertion(JMeterThread.java:949) [ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.threads.JMeterThread.checkAssertions(JMeterThread.java:918) [ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:586) [ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:501) [ApacheJMeter_core.jar:5.6.3] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:268) [ApacheJMeter_core.jar:5.6.3] at java.base/java.lang.Thread.run(Thread.java:834) [?:?] Caused by: java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[?:?] at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:162) ~[ApacheJMeter_core.jar:5.6.3] ... 10 more Caused by: bsh.TargetError: Sourced file: inline evaluation of: String dependentFeatID = ""; dependentFeatID = vars.get("dependentFeatID"); St . . . '' : Typed variable declaration at bsh.UtilTargetError.toEvalError(UtilTargetError.java:64) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.UtilEvalError.toEvalError(UtilEvalError.java:84) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:96) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:41) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.BSHVariableDeclarator.eval(BSHVariableDeclarator.java:80) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.BSHTypedVariableDeclaration.eval(BSHTypedVariableDeclaration.java:78) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.Interpreter.eval(Interpreter.java:659) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.Interpreter.eval(Interpreter.java:750) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.Interpreter.eval(Interpreter.java:739) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[?:?] at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:162) ~[ApacheJMeter_core.jar:5.6.3] ... 10 more Caused by: java.lang.NullPointerException: Null Pointer in Method Invocation at bsh.Name.invokeMethod(Name.java:838) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:69) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:96) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:41) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.BSHVariableDeclarator.eval(BSHVariableDeclarator.java:80) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.BSHTypedVariableDeclaration.eval(BSHTypedVariableDeclaration.java:78) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.Interpreter.eval(Interpreter.java:659) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.Interpreter.eval(Interpreter.java:750) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at bsh.Interpreter.eval(Interpreter.java:739) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[?:?] at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:162) ~[ApacheJMeter_core.jar:5.6.3] ... 10 more


Solution

  • If you're using Beanshell each statement must end with a semicolon, to wit instead of:

    String dependentFeatID = vars.get("dependentFeatID")
    

    you need to use

    String dependentFeatID = vars.get("dependentFeatID");
    

    and so on

    However a better idea would be switching to Groovy language, this way you won't have to add semicolons as Groovy is more tolerant. Moreover it's recommended to use Groovy for scripting since JMeter 3.1

    More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?