jmeter Beanshell - Error invoking bsh method: eval

32,580

Solution 1

Given you have the try block already - look into jmeter.log file for the "normal" stacktrace, this Error invoking bsh method: eval crap says nothing about the root cause. If you won't be able to figure the problem yourself - post the log part starting with Errror in Beanshell until the end here.

I can assume 2 possible reasons:

  1. One of your variables is not set due to being null (most likely), check the variables values by placing Debug Sampler before the Beanshell test element and double check the variables values in the View Results Tree listener.
  2. You don't have write access to /tmp/result.txt file (unlikely but also possible)

A couple of other recommendations:

  1. The best way of storing JMeter Variables values is using Sample Variables property
  2. If you have to go for scripting use JSR223 Test Elements and Groovy language instead of Beanshell.

Solution 2

The problem here is that you are using two post processors on the same sampler which are executed in wrong order. I presume you have something like:

  • Sampler 1
    • Regex/Beanshell extractor
  • Sampler 2
    • Regex/Beanshell extractor
  • Sampler 3
    • Beanshell post processor
    • Regex/Beanshell extractor

On third sampler, beanshell with script you have provided is trying to access variable which was not yet been initialized.

To resolve this problem, you need to move beanshell post processor bellow Regex/Beanshell extractor or move your script to new Beanshell Sampler placed after Sampler 3.

For more info about execution order of elements refer to this link.

Share:
32,580
Wojtas.Zet
Author by

Wojtas.Zet

Updated on July 08, 2022

Comments

  • Wojtas.Zet
    Wojtas.Zet almost 2 years

    I am trying to print 3 variables from 3 different HTTP requests in same Thread Group . I wrote following BeanShell in Jmeter:

    try {
        hash1 = vars.get("var_Hash_1");
        hash2 = vars.get("var_Hash_2");
        hash3 = vars.get("var_Hash_3");
        FileWriter fstream = new FileWriter("/tmp/result.txt",true);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(hash1);
        out.write(",");
        out.write(hash2);
        out.write(",");
        out.write(hash3);
        out.write(",");
        out.write("\n");
        out.close();
        fstream.close();
    }
    catch (Throwable e) {
        log.error("Errror in Beanshell", e);
        throw e;
    }
    

    And the exception is:

    2017/04/26 16:16:25 WARN - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``try { hash1 = vars.get("var_Hash_1"); hash2 = vars.get("var_Hash_2"); hash3 = va . . . '' : TargetError

    Whats intersting is that if I try to write only hash1 and hash2 same exception occurs but there is something written out to a result.txt file (hash1,hash2) with hash1,hash2,hash3 nothing is written out.

    All 3 variables shall exists as I execute 3 similar request and they are successfull. Any ideas?

    Edited: Log file from exception:

    2017/04/26 17:30:29 ERROR - jmeter.util.BeanShellTestElement: Errror in Beanshell java.lang.NullPointerException
            at java.io.Writer.write(Writer.java:127)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:483)
            at bsh.Reflect.invokeMethod(Reflect.java:134)
            at bsh.Reflect.invokeObjectMethod(Reflect.java:80)
            at bsh.Name.invokeMethod(Name.java:858)
            at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:75)
            at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)
            at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
            at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
            at bsh.BSHBlock.eval(BSHBlock.java:80)
            at bsh.BSHBlock.eval(BSHBlock.java:46)
            at bsh.BSHTryStatement.eval(BSHTryStatement.java:86)
            at bsh.Interpreter.eval(Interpreter.java:645)
            at bsh.Interpreter.eval(Interpreter.java:739)
            at bsh.Interpreter.eval(Interpreter.java:728)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:483)
            at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:170)
            at org.apache.jmeter.util.BeanShellInterpreter.eval(BeanShellInterpreter.java:197)
            at org.apache.jmeter.util.BeanShellTestElement.processFileOrScript(BeanShellTestElement.java:151)
            at org.apache.jmeter.extractor.BeanShellPostProcessor.process(BeanShellPostProcessor.java:64)
            at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:750)
            at org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:452)
            at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:261)
            at java.lang.Thread.run(Thread.java:745)
    
    • NaveenKumar Namachivayam
      NaveenKumar Namachivayam about 7 years
      I executed you code, it seems I am able to write the value to the file. I changed only this line FileWriter fstream = new FileWriter("C:/Projects/result.txt",true);
  • Wojtas.Zet
    Wojtas.Zet about 7 years
    Thanks Dmitri, Looks like variable hash3 is empty and thats the root cause. I have 3 almost identical HTTP samplers, and if beanshell script is placed at the end of Thread Group, I am able to access variable from sampler1 and sampler2. I am not able to access variable from sampler3, for unknown reason. If I place 3 different scripts in each sample separately without changing anything else, I have no exception and I am able to print all 3 variables.
  • Wojtas.Zet
    Wojtas.Zet about 7 years
    So it looks like Sampler1 and Sampler2 are in same scope but Sampler3 is not. I cant understand why, because they are all in same Thread Group
  • Wojtas.Zet
    Wojtas.Zet about 7 years
    Hi Ivan, although this is possible cause for such thing, I had my regex extractors / beanshell post processors in right order from the start
  • Iske
    Iske about 7 years
    I have tried setup from my answer. It doesn't work 'till I flipped Beanshell and Regex. Did you tried second option (putting script into Beanshell Sampler bellow samplers)?