Error running code in headless mode: java.lang.NumberFormatException: For input string:

Hi all,

I have a problem with a part of my code when running on linux in headless mode. The code works as expected when run in the GUI or using the -batch option. I have tried many different things (making the number an int, running the value with &startingImage, etc.) to no luck. Below is a snippet of the code and the error message.

Thanks in advance for your help!

Code

for (j=1; j<=iter+1;j++) {
startingImage=(j-1)*500+1;
run("Image Sequence...", "open=[filename] number=500 starting=startingImage increment=1 scale=100 file=.tif sort use");

Error Message

java.lang.NumberFormatException: For input string: "startingImage"
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
        at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
        at java.lang.Double.parseDouble(Double.java:538)
        at net.imagej.patcher.HeadlessGenericDialog.getMacroParameter(HeadlessGenericDialog.java:348)
        at net.imagej.patcher.HeadlessGenericDialog.addNumericField(HeadlessGenericDialog.java:129)
        at ij.plugin.FolderOpener.showDialog(FolderOpener.java:366)
        at ij.plugin.FolderOpener.run(FolderOpener.java:116)
        at ij.IJ.runPlugIn(IJ.java:186)
        at ij.Executer.runCommand(Executer.java:137)
        at ij.Executer.run(Executer.java:66)
        at ij.IJ.run(IJ.java:296)
        at ij.macro.Functions.doRun(Functions.java:601)
        at ij.macro.Functions.doFunction(Functions.java:96)
        at ij.macro.Interpreter.doStatement(Interpreter.java:230)
        at ij.macro.Interpreter.doBlock(Interpreter.java:608)
        at ij.macro.Interpreter.doStatement(Interpreter.java:272)
        at ij.macro.Interpreter.doFor(Interpreter.java:530)
        at ij.macro.Interpreter.doStatement(Interpreter.java:254)
        at ij.macro.Interpreter.doStatements(Interpreter.java:218)
        at ij.macro.Interpreter.run(Interpreter.java:115)
        at ij.macro.Interpreter.run(Interpreter.java:85)
        at ij.macro.Interpreter.run(Interpreter.java:96)
        at ij.plugin.Macro_Runner.runMacro(Macro_Runner.java:155)
        at ij.plugin.Macro_Runner.runMacroFile(Macro_Runner.java:139)
        at ij.IJ.runMacroFile(IJ.java:147)
        at net.imagej.legacy.IJ1Helper$4.call(IJ1Helper.java:988)
        at net.imagej.legacy.IJ1Helper$4.call(IJ1Helper.java:984)
        at net.imagej.legacy.IJ1Helper.runMacroFriendly(IJ1Helper.java:918)
        at net.imagej.legacy.IJ1Helper.runMacroFile(IJ1Helper.java:984)
        at net.imagej.legacy.LegacyCommandline$Macro.handle(LegacyCommandline.java:188)
        at org.scijava.console.DefaultConsoleService.processArgs(DefaultConsoleService.java:93)
        at net.imagej.legacy.LegacyConsoleService.processArgs(LegacyConsoleService.java:77)
        at org.scijava.AbstractGateway.launch(AbstractGateway.java:95)
        at net.imagej.Main.launch(Main.java:62)
        at net.imagej.Main.main(Main.java:68)
        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:497)
        at net.imagej.launcher.ClassLauncher.launch(ClassLauncher.java:279)
        at net.imagej.launcher.ClassLauncher.run(ClassLauncher.java:186)
        at net.imagej.launcher.ClassLauncher.main(ClassLauncher.java:77)

You should use string concatenation to safely generate your option string:

run("Image Sequence...", "open=[" + filename + "] number=500 starting=" + startingImage + " increment=1 scale=100 file=.tif sort use");
1 Like

Thanks @imagejan! I didn’t realize you could concatenate strings that way in .ijm macros. It works perfectly now…