相关文章推荐
绅士的鸡蛋面  ·  Hadoop之——Permission ...·  3 年前    · 
胆小的豆浆  ·  System.Windows.Markup. ...·  3 年前    · 
性感的绿豆  ·  Azure subscription ...·  3 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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)
                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);
– NaveenKumar Namachivayam
                Apr 26, 2017 at 18:20

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:

  • 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.
  • You don't have write access to /tmp/result.txt file (unlikely but also possible)
  • 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 Apr 26, 2017 at 16:20 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 Apr 26, 2017 at 16:27

    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.

    Hi Ivan, although this is possible cause for such thing, I had my regex extractors / beanshell post processors in right order from the start – Wojtas.Zet May 3, 2017 at 11:08 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)? – Iske May 3, 2017 at 11:43

    Check the value by adding debug sampler or use following code in beanshell script to make sure you are getting all values

     log.info("Hash1 value :"+hash1);
     log.info("Hash2 value :"+hash2);
     log.info("Hash3 value :"+hash3);
    

    if not then check order of your regular expression extractors.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid …

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.