Android Question 'App keeps stopping' message

TonyVerberne

Member
Licensed User
Longtime User
I have been using the Tables and Forms method published by Erel in April 2019. It works well and I am modifying it to make a Cheese Making Diary (my other hobby). When I compile the original code it works perfectly well but when I delete an item in the form and the re-open and close the app I get the message "B4A Example keeps stopping".
I have included all necessary libraries. Any suggestions as to why this occurs?
 

mangojack

Expert
Licensed User
Longtime User
Without supplying any code or a sample project , it is near impossible to help with your issue.

Possible upload your project or at least a stripped version showing the issue. IDE > File > Export as Zip
 
Upvote 0

TonyVerberne

Member
Licensed User
Longtime User
I have attached the zip file of the unmodified code. When I add an item and then delete it, closing the app brings the error message. Thanks for your help.
 

Attachments

  • TableAndForms.zip
    189.3 KB · Views: 226
Upvote 0

mangojack

Expert
Licensed User
Longtime User
The supplied project runs as expected without any issues.

The zip you supplied appears to be the example from @Erel here ... b4x Editable B4XTable & Form example

The only changes I can see is the addition of some Cheeses Types to the Animal.txt file.
As I said ... all appears OK .. Are you sure you have not made other changes to YOUR project.

Are you testing this in Debug mode ? ... There should be some form error message in the Logs . If so , post it.
 
Last edited:
Upvote 0

TonyVerberne

Member
Licensed User
Longtime User
Yes MangoJack, it is Erel's code. Here is the error message as listed in Debug mode:
Error occurred on 139 (EditableTable)
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Plus a lot of other stuff.
Thanks for your help.
 
Upvote 0

TonyVerberne

Member
Licensed User
Longtime User
Thanks Erel. Just to reiterate: this error occurs when I add an item, close the app, reopen the app, delete the item and then close.
Here it is:
Logger connected to: Google Pixel
--------- beginning of main
Copying updated assets files (20)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Index time: 37 ms (497 Items)
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
Error occurred on line: 139 (EditableTable)
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)
at anywheresoftware.b4a.objects.StringUtils.SaveCSV2(StringUtils.java:107)
at anywheresoftware.b4a.objects.StringUtils.SaveCSV(StringUtils.java:100)
at b4a.example.editabletable._exporttabletocsv(editabletable.java:187)
at b4a.example.main._activity_pause(main.java:420)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example.main.onPause(main.java:271)
at android.app.Activity.performPause(Activity.java:7978)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1507)
at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4493)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4454)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4406)
at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:46)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Taking another look I can confirm your issue ..

Using This example ... b4x example B4XTable & Forms Example ...

If you add 1 entry to the table , close the project , restart project and then delete entry from table ... when you close the project it will Error.

** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
Error occurred on line: 139 (EditableTable)
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0


In Main Module ..
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    et.ExportTableToCSV
End Sub

EditableTable Class
B4X:
Public Sub ExportTableToCSV
    Dim data As List
    data.Initialize
    Dim rs As ResultSet = B4XTable1.sql1.ExecQuery("SELECT * FROM data")
    Do While rs.NextRow
        Dim row(B4XTable1.Columns.Size) As String
        For i = 0 To B4XTable1.Columns.Size - 1
            Dim c As B4XTableColumn = B4XTable1.Columns.Get(i)
            row(i) = rs.GetString(c.SQLID)
        Next
        data.Add(row)
    Loop
    rs.Close
    Dim su As StringUtils

    su.SaveCSV(xui.DefaultFolder, CSVFile, ",", data)  '  ERROR Line #139  ***

End Sub

Still having a look for a possible solution re: closing with an empty table.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Overwriting the ExportTableToCSV sub (in class EditableTable) with the following code solves the issue ...

B4X:
Public Sub ExportTableToCSV
    Dim data As List
    data.Initialize
    Dim rs As ResultSet = B4XTable1.sql1.ExecQuery("SELECT * FROM data")
   
    If rs.RowCount > 0 Then
        Do While rs.NextRow
            Dim row(B4XTable1.Columns.Size) As String
            For i = 0 To B4XTable1.Columns.Size - 1
                Dim c As B4XTableColumn = B4XTable1.Columns.Get(i)
                row(i) = rs.GetString(c.SQLID)
            Next
            data.Add(row)
        Loop
        rs.Close
        Dim su As StringUtils
        su.SaveCSV(xui.DefaultFolder, CSVFile, ",", data)  'Errors out if data list is empty !
   
    Else
            File.Delete(xui.DefaultFolder, CSVFile) 'Delete CSV data file
    End If
End Sub

Basically it tests for ResultSet (Table entries) Count first.
If the Table has entries it will write them to the CSVFile ... Else If the table is empty, then it will just delete the CSV File. (as it cannot write an empty list to the file)

Unsure if @Erel has a better method to handle this.
 
Upvote 0

TonyVerberne

Member
Licensed User
Longtime User
Thank you MJ. This has completely solved the problem. So now the code only writes to the CSV file when at least one row is populated with data.
Cheers Tony
 
Upvote 0
Top