1
votes

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)
3
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

3 Answers

1
votes

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.
1
votes

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.

0
votes

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.