JMeter/BeanShell:类型化变量声明:数组初始值设定项错误:空

JMeter/BeanShell: Typed variable declaration : Error in array initializer: null

我对这里的代码有疑问 https://www.blazemeter.com/blog/saving-data-to-csv-files-with-java-through-jmeter 我尝试将数据写入 csv。我使用所有代码 - 我只更改第 47 行 String[] 参数 = {${context1}, ${context2}};

当我执行测试时,我在采样器结果中收到这样的消息

Response code: 500

Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: 'import java.io.FileWriter; import java.util.Arrays; import java.io.Writer; impor . . . ' : Typed variable declaration : Error in array initializer: null in log file

2018/02/01 12:32:52 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: 'import java.io.FileWriter; import java.util.Arrays; import java.io.Writer; impor . . . ' : Typed variable declaration : Error in array initializer: null

2018/02/01 12:32:52 WARN - jmeter.protocol.java.sampler.BeanShellSampler: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: 'import java.io.FileWriter; import java.util.Arrays; import java.io.Writer; impor . . . ' : Typed variable declaration : Error in array initializer: null

有什么帮助吗? :)

    import java.io.FileWriter;
import java.util.Arrays;
import java.io.Writer;
import java.util.List;

//Default separator
char SEPARATOR = ',';

//function write line in csv
public void writeLine(FileWriter writer, String[] params, char separator)
{
   boolean firstParam = true;

   StringBuilder stringBuilder = new StringBuilder();
   String param = "";

   for (int i = 0; i < params.length; i++)
   {
      //get param
      param = params[i];
      log.info(param);

         //if the first param in the line, separator is not needed
       if (!firstParam) 
       {
           stringBuilder.append(separator);
       }

         //Add param to line
       stringBuilder.append(param);

       firstParam = false;
   }

   //prepare file to next line
   stringBuilder.append("\n");

   //add to file the line
   log.info(stringBuilder.toString());
   writer.append(stringBuilder.toString());

}

//get path of csv file (creates new one if its not exists)
String csvFile = "/JMeter/dane.csv"; // for example '/User/Downloads/blabla.csv'

String[] params = {${context1}, ${context2}};

FileWriter fileWriter = new FileWriter(csvFile, true);
writeLine(fileWriter, params, SEPARATOR);

//proper close to file
fileWriter.flush();
fileWriter.close();
  1. 使用 Debug Sampler and View Results Tree 侦听器组合
  2. 仔细检查您的 context1context2 变量值
  3. 将第 47 行修改为:

    String[] params = {"${context1}", "${context2}"};
    
  4. 如果您仍然遇到问题,请尝试在 try block 中包围您的代码,例如:

    try {
        //your code here
    }
    catch (Throwable ex) {
       log.info("Error in Beanshell", ex);
       throw ex;
    }
    

    这样您将在 jmeter.log 文件中获得 "human readable" 堆栈跟踪。

有关 JMeter 中 Beanshell 脚本的更多信息,请参阅 How to Use BeanShell: JMeter's Favorite Built-in Component 文章。