user3871995
user3871995

Reputation: 1213

Jmeter error when trying to create file using beanshell

This is my beanshell code to create a file and append one line to it:

FileName = vars.get("fileName");

f = new FileOutputStream(FileName,true);
p = new PrintStream(f); 
this.interpreter.setOut(p); 


print("Test Set: " + FileName);

f.close();

I get fileName from a regex extractor in a previous sampler. I have checked debug postprocessor and confirmed this is set correctly. However I get this error in sampler result:

Response code: 500
Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh     method: eval  Sourced file: inline evaluation of: ``FileName = vars.get("fileName");  f = new FileOutputStream(FileNam . . . '' : Object constructor

Upvotes: 0

Views: 442

Answers (1)

timbre timbre
timbre timbre

Reputation: 13960

The problem is: if FileName is null, the constructor for FileOutputStream will throw an exception, and BeanShell is not great in showing underlying exceptions. So what you need is to handle the case where file name is null:

String fileName = vars.get("fileName");

if( fileName == null )
{
    fileName = "mydefaultname"; // assign some default name
}

f = new FileOutputStream(fileName, true);
p = new PrintStream(f); 
this.interpreter.setOut(p); 


print("Test Set: " + fileName);

f.close();

If you don't want to have some default name, you can also quit the script at that point:

if( fileName == null )
{
    return;
}

Upvotes: 2

Related Questions