FireMonkey Android: raised exception class EJNIException with message ‘java.lang.IndexOutOfBoundsException: setSpan (XX … XX) ends beyond length X

  

I have 2 TNumberBox controls in my page, and their property changes are as follows:
MaxValue : 1E9
DecimalDigits: 2
Text: 0.00
KeyBoardType: DecimalNumberpad
Also, in both boxes, I call SelectAll() in the OnClick event, eg:
procedure TfrmExpenseClaim.numTaxClick(Sender: TObject);
begin
numTax.SelectAll;
end;

I also have a TComboBox on the page. I can enter a value on the first TNumberBox and then, based on a certain TComboBox value, the second TNumberBox will get updated. Also, I can update the second TNumberBox value manually, which does not have an effect on the first TNumberBox.
First Number Box value : 110
Combo Box value : 10% tax
Second TNumberBox value gets updated as 10 = ( NumberBox1.Value – ( NumberBox1.Value / ( 1 + 10/ 100))
The calculations work fine.
The problem is, if I change the second TNumberBox value manually, and then quickly change the TComboBox value again and again, and then select the second TNumberBox, it will display the following error:

Project ExpenseClaims.apk raised exception class EJNIException with message ‘java.lang.IndexOutOfBoundsException: setSpan (6 … 6) ends beyond length 4’.

The setSpan (6 … 6) changes, as sometimes it displays (5 … 5) and (4 … 4). Afterwards, most of the time when I select that second TNumberBox, I will get that error and the values also get updated incorrectly.
The only way to recover after getting this error is to close the app and open it again.
I tried to capture that error in code, but could not as it comes outside of the Delphi code that I have written.
Is this a known error? If so, what can I do to fix it?
type
TfrmExpenseClaim = class(TForm)
numTax: TNumberBox;
numInclTax: TNumberBox;
cmbTaxCategory: TComboBox;
procedure numInclTaxClick(Sender: TObject);
procedure numTaxClick(Sender: TObject);
procedure numInclTaxChange(Sender: TObject);
procedure cmbTaxCategoryChange(Sender: TObject);
private
FTaxDetailsList : TTaxDetailsList;
procedure CalculateTax;
end;

procedure TfrmExpenseClaim.cmbTaxCategoryChange(Sender: TObject);
begin
CalculateTax;
end;

procedure TfrmExpenseClaim.numInclTaxChange(Sender: TObject);
begin
if cmbTaxCategory.Selected.Text <> ” then
begin
CalculateTax;
end;
end;

procedure TfrmExpenseClaim.numInclTaxClick(Sender: TObject);
begin
numInclTax.SelectAll;
end;

procedure TfrmExpenseClaim.numTaxClick(Sender: TObject);
begin
//This is where the problem occur as I think, but not in delphi code
numTax.SelectAll;
end;

procedure TfrmExpenseClaim.CalculateTax;
var
I: Integer;
var
dPriceEx, dTaxVal: Double;
begin
dPriceEx := 0.00;
for I := 0 to FTaxDetailsList.Count – 1 do
begin
// Filling up the FTaxDetailsList work fine which is done via a API call
if (cmbTaxCategory.Selected.Text = FTaxDetailsList[I].RateTitle) then
begin
if FTaxDetailsList[I].TaxRate = 0.00 then
begin
numTax.Value := 0.00;
end
else
begin
dPriceEx := numInclTax.Value / (1 + FTaxDetailsList[I].TaxRate / 100);
dTaxVal := numInclTax.Value;
numTax.Value := dTaxVal – dPriceEx;
// Example 1 : numInclTax.Value is 110.00 and the FTaxDetailsList[I].TaxRate is 10 (10%). Therefore the numTax.Value should be 10.00;
// Example 2 : numInclTax.Value is 110.00 and the FTaxDetailsList[I].TaxRate is 0 (0%). Therefore the numTax.Value should be 0.00;
end;
end;
end;
end;

The problem won’t occur in the first instance. Maybe not until the 5th or 6th instance. But it will occur after a few quick changes.
How to produce:
First set of Steps:

Enter a value to numInclTax.Value (eg : 115.00)

By default, the cmbTaxCategory is set to 15% GST. Meaning 15% tax rate.

It will calculate the numTax.Value based on the TfrmExpenseClaim.CalculateTax code to 15.00

This works fine.
Second set of Steps:

Then change the cmbTaxCategory option to Zero based tax (which is no tax)

It will calculate the numTax.Value based on the TfrmExpenseClaim.CalculateTax code to 0

Works fine.
Third set of Steps:

Then update the numTax.Value by swapping across the Number box. It will either increase or reduce the numTax.Value based on the direction you swap (I never went to minus values)

Then again change the cmbTaxCategory option, again it should calculate the values.

Then numTax.Value by swapping across the Number box again.

At some point, by repeating the Third set of Steps again and again, on the 3rd step as soon as you select the numTax.Value the error will occur.
I was only able to replicate this on Android. And I think it might be something to do with swapping across the number box. But I’m not sure.

Comments are closed.