We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

IndexOutOfBoundsException on updateFields() or save on updateFields

Using Aspose words for java version 21.12
I upgraded recently from 17.3.0 to 21.12 and for a particular report this exception happens when calling the updateFields() method on the document.

Any suggestions for workaround or versions that does not have this issue?

java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 0
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
	at java.base/java.util.Objects.checkIndex(Objects.java:359)
	at java.base/java.util.ArrayList.get(ArrayList.java:427)
	at com.aspose.words.ListLevelCollection.get(Unknown Source)
	at com.aspose.words.ListLevelCollection.zzZYg(Unknown Source)
	at com.aspose.words.List.zzl3(Unknown Source)
	at com.aspose.words.ListCollection.zzZp8(Unknown Source)
	at com.aspose.words.Paragraph.zzZhV(Unknown Source)
	at com.aspose.words.Paragraph.fetchInheritedParaAttr(Unknown Source)
	at com.aspose.words.ParagraphFormat.fetchInheritedShadingAttr(Unknown Source)
	at com.aspose.words.Shading.zzZM1(Unknown Source)
	at com.aspose.words.Shading.getTexture(Unknown Source)
	at com.aspose.words.zzW40.zzVR4(Unknown Source)
	at com.aspose.words.zzW40.visitParagraphStart(Unknown Source)
	at com.aspose.words.Paragraph.zzZp8(Unknown Source)
	at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
	at com.aspose.words.Paragraph.accept(Unknown Source)
	at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
	at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
	at com.aspose.words.Cell.accept(Unknown Source)
	at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
	at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
	at com.aspose.words.Row.accept(Unknown Source)
	at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
	at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
	at com.aspose.words.Table.accept(Unknown Source)
	at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
	at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
	at com.aspose.words.Body.accept(Unknown Source)
	at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
	at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
	at com.aspose.words.Section.accept(Unknown Source)
	at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
	at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
	at com.aspose.words.Document.accept(Unknown Source)
	at com.aspose.words.zzW40.zzWxF(Unknown Source)
	at com.aspose.words.zzZ9c.zzYVX(Unknown Source)
	at com.aspose.words.Document.updatePageLayout(Unknown Source)
	at com.aspose.words.zzWlQ.zzVUt(Unknown Source)
	at com.aspose.words.zzhL.zzYuK(Unknown Source)
	at com.aspose.words.zzWU9.zza9(Unknown Source)
	at com.aspose.words.zzWU9.zzZqK(Unknown Source)
	at com.aspose.words.zzWU9.zzZp8(Unknown Source)
	at com.aspose.words.zzWU9.zzXEq(Unknown Source)
	at com.aspose.words.Range.updateFields(Unknown Source)
	at com.aspose.words.Document.updateFields(Unknown Source)

@Bjorkli Could you please attach your report document? We will check it and provide you more information.

I have a solution so this information is just for completeness of this ticket.

Unfortunately It is not as simple as providing a document. Our aspose code is quite complicated and involves copying elements from one document to another.

I have researched this problem further and isolated the problem to copying List from the source to the destination.

if the destination document does not have the same list as the source it is copied.

List newList = getOut().getLists().addCopy(inList);
getBuilder().getListFormat().setList(newList);
copyFormat(in.getListFormat(), getBuilder().getListFormat());

Somehow the above code does not fail with the inlist not belonging to a different document. In the tests I made for reproduce this they do so I’m not sure what is going on.

I fixed the problem by replacing the above code with

Style outStyle = getOut().getStyles().get(inList.getStyle().getName());
if (outStyle != null) {
   outStyle = getOut().getStyles().addCopy(inList.getStyle());
}
List newList = getOut().getLists().add(outStyle);

In his the Style is copied instead and a new list that belongs to the out document is made. This avoids the problem.
The old code worked before we upgraded from the old version to 21.12 so there might be a regression there for other customers.

@Bjorkli Glad you was able to fix your problem. Unfortunately I cannot reproduce the problem on my side without test documents and complete code example so I cannot report it to our development team.