ArrayIndexOutOfBoundsException from Workbook.Write

GeromeCooke

New Member
Licensed User
Longtime User
Hi

I am developing a ordering system for my client. The products have to be split to different Excel Files when they meet a criteria. Thus I'm loading 1 Template and Writing two copies each with the individual data.

I'm loading a Readable Workbook with the following code
B4X:
Dim A_ReadableWB As ReadableWorkbook
Dim A_ROrderSheet As ReadableSheet
A_ReadableWB.Initialize("/storage/emulated/0/Folder/Template/", "Template.xls")
A_ROrderSheet = A_ReadableWB.GetSheet(0)
   
Dim B_ReadableWB As ReadableWorkbook
Dim B_ROrderSheet As ReadableSheet
B_ReadableWB.Initialize("/storage/emulated/0/Folder/Template/", "Template.xls")
B_ROrderSheet = B_ReadableWB.GetSheet(0)

As you can see, I'm loading the some Template twice but using two different Readable Sheets. And then defining the Writable Workbook as follows.

B4X:
AFileName = "Order A" 
BFileName = "Order B"
      
Dim A_WritableWB As WritableWorkbook
ICONWritableWB.Initialize2("/storage/emulated/0/Folder/Orders/", AFilename & ".xls",A_ReadableWB)
Dim A_WOrdersheet As WritableSheet
A_WOrdersheet = A_WritableWB.GetSheet(0)
   
Dim B_WritableWB As WritableWorkbook
Dim B_WritableWB.Initialize2("/storage/emulated/0/Folder/Orders/", BFileName & ".xls",B_ReadableWB)
Dim B_WOrdersheet As WritableSheet
B_WOrdersheet = B_WritableWB.GetSheet(0)

Problem comes in when populating the Excel files. The first one is fine but I get an error on the second one. Please see code below.

B4X:
SQL.Initialize("/storage/emulated/0/Folder/DataBase/", "DB.db", True)
      
Dim CursorProducts As Cursor
CursorProducts = SQL.ExecQuery("SELECT Code, Company, Description, Category, Units, PriceListA, PriceListB, PriceListC FROM Products")

Dim ACount As Int
Dim BCount As Int

ACount = BCount = 0
   
For i = 0 To FOrdersLV.GetSize - 1
   Code = FOrdersLV.GetPanel(i).GetView(0)
   QtyEdt = FOrdersLV.GetPanel(i).GetView(4)
      
   For j = 0 To CursorProducts.RowCount - 1
      CursorProducts.Position = j
         
      If Code.text = CursorProducts.GetString("Code") Then
         Company = CursorProducts.GetString("Company")
            
         If Company = "A" Then
            AVar = "True"
            
                 ACell.InitializeText(0,18 + ACount, QtyEdt.text)
                 ACount = ACount + 1
            
         Else If Company = "B" Then
            BVar = "True"
               
            BCell.InitializeText(0,18 + BCount, QtyEdt.text)
            B_WOrdersheet.AddCell(BCell)
      
            BCount = BCount + 1 'Probably the cause of the error
         End If
      Next
   Next
   
   If AVar = "True" Then
      A_WritableWB.Write 'No Problem Here
      A_WritableWB.Close
   Else
      A_WritableWB.Close   
   End If
   
   If  BVar = "True" Then
      B_WritableWB.Write 'Error Happens Here
      B_WritableWB.Close
   Else
      B_WritableWB.Close
   End If

I'm am getting the following Error Message and I have no clue???

B4X:
java.lang.ArrayIndexOutOfBoundsException: length=27; index=27

No Clue Where the "27" comes from?

PLEASE HELP.

Regards Gerome
 

GeromeCooke

New Member
Licensed User
Longtime User
Hi Erel

Thank you for your prompt response.

I did run the program in Debug Mode and found the error to occur at the point indicated below. I do not understand why it works with the first .Write instance but not the second.

B4X:
If AVar = "True" Then
        A_WritableWB.Write 'No Problem Here
        A_WritableWB.Close
    Else
        A_WritableWB.Close    
    End If
    
    If  BVar = "True" Then
        B_WritableWB.Write 'Error Happens Here
        B_WritableWB.Close
    Else
        B_WritableWB.Close
    End If

Furthermore, Please find the whole error message below.

B4X:
java.lang.ArrayIndexOutOfBoundsException: length=27; index=27
   at jxl.biff.IndexMapping.getNewIndex(IndexMapping.java:68)
   at jxl.biff.FormattingRecords.rationalize(FormattingRecords.java:388)
   at jxl.write.biff.WritableWorkbookImpl.rationalize(WritableWorkbookImpl.java:1023)
   at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:701)
   at anywheresoftware.b4a.objects.WorkbookWrapper$WritableWorkbookWrapper.Write(WorkbookWrapper.java:149)
   at Icon.Menu.order._sendorderbtn_click(order.java:1763)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:63)
   at android.view.View.performClick(View.java:4204)
   at android.view.View$PerformClick.run(View.java:17355)
   at android.os.Handler.handleCallback(Handler.java:725)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:5041)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.ArrayIndexOutOfBoundsException: length=27; index=27

Any help would be appreciated.

Regards
 
Upvote 0

GeromeCooke

New Member
Licensed User
Longtime User
Resolved

Hi Erel

I have found a alternate solution by writing the variables in a Temporary SQLite Table and populating it from there individually.

It still gave the same error however if you try and use two Excel.Write functions.

Regards

Gerome
 
Upvote 0
Top