java.lang.ArrayIndexOutOfBoundsException: 0 with ImageJ 1.52p

When using the Fit tool for gaussian fit, I occasionally get the following error. Is there a way to understand what could be wrong?
This issue was not present when I run the macro with ImageJ 1.49v (I did not test the intermediate versions). I also tried the daily build (1.52q20) with the same result.

java.lang.ArrayIndexOutOfBoundsException: 0
	at ij.measure.CurveFitter.makeInitialParamsAndVariations(CurveFitter.java:927)
	at ij.measure.CurveFitter.doFit(CurveFitter.java:201)
	at ij.macro.Functions.fitCurve(Functions.java:5558)
	at ij.macro.Functions.fit(Functions.java:5482)
	at ij.macro.Functions.getFunctionValue(Functions.java:238)
	at ij.macro.Interpreter.getFactor(Interpreter.java:1512)
	at ij.macro.Interpreter.getTerm(Interpreter.java:1483)
	at ij.macro.Interpreter.getStringExpression(Interpreter.java:1636)
	at ij.macro.Interpreter.getStringTerm(Interpreter.java:1440)
	at ij.macro.Interpreter.getString(Interpreter.java:1385)
	at ij.macro.Interpreter.doStatement(Interpreter.java:329)
	at ij.macro.Interpreter.doBlock(Interpreter.java:671)
	at ij.macro.Interpreter.doStatement(Interpreter.java:320)
	at ij.macro.Interpreter.doIf(Interpreter.java:1049)
	at ij.macro.Interpreter.doStatement(Interpreter.java:296)
	at ij.macro.Interpreter.doBlock(Interpreter.java:671)
	at ij.macro.Interpreter.doStatement(Interpreter.java:320)
	at ij.macro.Interpreter.doIf(Interpreter.java:1062)
	at ij.macro.Interpreter.doStatement(Interpreter.java:296)
	at ij.macro.Interpreter.doBlock(Interpreter.java:671)
	at ij.macro.Interpreter.doStatement(Interpreter.java:320)
	at ij.macro.Interpreter.doIf(Interpreter.java:1049)
	at ij.macro.Interpreter.doStatement(Interpreter.java:296)
	at ij.macro.Interpreter.doBlock(Interpreter.java:671)
	at ij.macro.Interpreter.doStatement(Interpreter.java:320)
	at ij.macro.Interpreter.doFor(Interpreter.java:593)
	at ij.macro.Interpreter.doStatement(Interpreter.java:302)
	at ij.macro.Interpreter.doStatements(Interpreter.java:261)
	at ij.macro.Interpreter.run(Interpreter.java:157)
	at ij.macro.Interpreter.run(Interpreter.java:91)
	at ij.macro.MacroRunner.run(MacroRunner.java:139)
	at java.lang.Thread.run(Thread.java:745)

@Sara_Vecchio Welcome to the forum! :smile:

At first glance, this behavior looks like a bug.

Do you have a short macro you can post that reproduces the issue? Does it always happen with particular input parameters? Or does it seem more random?

If we can reproduce it, we can debug it.

Thank you for your reply.
While trying to simplifying the macro for your review, I found out that the issue appears when the input vector contains NaN (the same vector had normal values with version 1.49). I will keep debugging by myself, still I found out a very different behaviour of vector indexes and functions between v1.49 and v 1.52 !!

1 Like

Hello Sara -

I had tried to reproduce your ArrayIndexOutOfBoundsException
– without success – but your information about the NaNs has
shown me the true path to success.

(I was able to induce the exception by instantiating CurveFitter
directly with zero-length arrays, but that’s not the point.)

This ijm script reproduces the exception:

x = newArray (NaN, NaN);
y = newArray (NaN, NaN);
Fit.logResults;
print ("fitting line ...");
Fit.doFit ("Straight Line", x, y);  // no exception
print ("fitting gaussian ...");
Fit.doFit ("Gaussian", x, y);       // throws exception

(I’m running 1.52p.)

The functions that call CurveFitter seem pretty well protected
against bad input, but I don’t see them checking for NaNs. But I
can’t, for the life of me, see how the NaNs end up somehow
generating a zero-length array.

Also, fitting “Straight Line” (with NaNs) doesn’t throw the exception,
but fitting “Gaussian” does.

Thanks, mm

You are probably getting this exception because the curve fitter in ImageJ 1.52p removes NaNs. Thanks to Michael Schmid, the latest ImageJ daily build (1.52q26) displays a “No data to fit” error when all data points have been removed.