0
votes

i have a problem with code from here https://www.blazemeter.com/blog/saving-data-to-csv-files-with-java-through-jmeter I try to write data to csv. I use all code - i only change line 47 String[] params = {${context1}, ${context2}};

when i execute the test i get such a message in sampler result

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

any help ? :)

    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
Can you show you code in this component ? thxUBIK LOAD PACK

1 Answers

-1
votes
  1. Double check that your context1 and context2 variable values using Debug Sampler and View Results Tree listener combination
  2. Amend your line 47 to look like:

    String[] params = {"${context1}", "${context2}"};
    
  3. If you will still face problems try surrounding your code in the try block like:

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

    This way you will get a "human readable" stacktrace in jmeter.log file.

See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on Beanshell scripting in JMeter.