Android Question AbstractStringBuilder.enlargeBuffer error

tcgoh

Active Member
Licensed User
Longtime User
Hi,

I have a project which is running nicely on samsung S5 and above phone, but on S3 phone when I run the module it show ""Unfortunately the-APP has stopped". The following error shown on the log
B4X:
java.lang.OutOfMemoryError
    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
    at java.lang.StringBuilder.append(StringBuilder.java:216)
    at rosteroid2.tc.condata._populatelist(condata.java:733)
    at rosteroid2.tc.condata._activity_create(condata.java:375)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at rosteroid2.tc.condata.afterFirstLayout(condata.java:102)
    at rosteroid2.tc.condata.access$000(condata.java:17)
    at rosteroid2.tc.condata$WaitForLayout.run(condata.java:80)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    at dalvik.system.NativeStart.main(Native Method)

Anyone known what's wrong ? Thanks
 

tcgoh

Active Member
Licensed User
Longtime User
Can you post the code in PopulateList sub?

My code in populateList,
B4X:
Sub populateList

If File.Exists(File.DirRootExternal,"all.txt") Then
          Dim Reader1 As  TextReader
        If Reader1.IsInitialized = False Then
              Reader1.Initialize(File.OpenInput(File.DirRootExternal,"all.txt"))
        End If
    End If
   
        Dim list2 As List
        list2.Initialize 
        Dim LN As Int    
        Dim line As String
       
        line=Reader1.ReadLine
                
        Do While line <> Null
       
        line = line.Replace(",", " ")  ' replace , in the crew name
        list2.Add(line)
        listview1.AddSingleLine(line)
           
        line=Reader1.ReadLine
           
        Loop
        Reader1.Close
   
        LN = listview1.Size
        'list2.RemoveAt(LN-1)
        list2.RemoveAt(LN-2)   ' remove the last line with<p>
         list2.RemoveAt(0)      ' remove the 1st few empty lines and  <p>
        list2.RemoveAt(0)
        list2.RemoveAt(0)
           
     File.WriteList(File.DirRootExternal, "alltxtdata.txt", list2)

        list2.IsInitialized

Dim list1 As List
list1.Initialize
If File.Exists(File.DirRootExternal,"alltxtdata.txt")=True Then
    Dim Reader2 As TextReader
    If Reader2.IsInitialized = False Then
        Reader2.Initialize(File.OpenInput(File.DirRootExternal,"alltxtdata.txt"))
    End If
     
    'Dim line As String
   
    Dim rsb As StringBuilder
    line=Reader2.ReadLine 

    
     Do While line.Length >0
        rsb.Initialize 
        rsb.Append(line)
        rsb.Insert(9,",") 
        rsb.Insert(33,",")
        rsb.Insert(37,",")
        rsb.Insert(42,",")
        rsb.Insert(48,",")
        rsb.Insert(58,",")
        rsb.Insert(64,",")
        rsb.Insert(75,",")
        rsb.Insert(85,",")
        DoEvents
    Dim  CC() As String  'CC is  parsed elements content
    Dim Line2 As String
   
   
        Line2=rsb.ToString
        CC = Regex.Split(",", Line2)  'field separator is comma
        Line2=CC(0).Trim & "," & CC(1).Trim & "," & CC(2).Trim & "," & CC(3).Trim & "," & CC(4).Trim & "," & _
                CC(5).Trim & "," & CC(6).Trim & "," & CC(7).Trim & "," & CC(8).Trim    & "," & CC(9).Trim                
        list1.Add(Line2)
        tempListView.AddSingleLine (Line2) 
        line=Reader2.ReadLine
      Loop

      Reader2.Close
     
     ProgressDialogHide
         
       
      File.WriteList(File.DirRootExternal, "allListgd.txt", list1)
  '         Msgbox("Ready for Part 2 conversion", "  PART  1 COMPLETED")
  Else
    Msgbox("file does not exist","error")
  End If

    button3.Visible = True
End Sub
Thanks
 
Upvote 0

tcgoh

Active Member
Licensed User
Longtime User
all.txt file is about 2.5 mb and alltxtdata.txt is just a rewrite of all.txt file, which should be about 2.5mb also.

I will try the debug mode. Thanks
 
Upvote 0

tcgoh

Active Member
Licensed User
Longtime User
If you like upload a small project with the data and the code that parses the file. I'm sure that it can be optimized.
Hi,

Here is a small project for the above problem. Please take note the actual all.txt file is about 2.9mb as compare to the zip file all.txt file of 16kb.

Thanks for any help.
 

Attachments

  • condata_ex.zip
    17.1 KB · Views: 229
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try it with this code:
B4X:
Sub Process_Globals
   Type Record (SN As String , NAME As String, FL As String, RK As String, COP As String _
     , DEP As String, TM As String,FLTNO As String, ARR As String, TM2 As String)
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     ParseList
   End If
End Sub

Sub ParseList As List
   Dim lines As List = File.ReadList(File.DirAssets, "all.txt")
   Dim res As List
   res.Initialize
   For Each line As String In lines
     Dim s() As String = Regex.Split("\s+", line)
     If s.Length < 10 Then Continue
     Dim r As Record
     r.Initialize
     Dim i As Int = 0
     r.SN = s(i)
     i = i + 1
     r.NAME = s(i)
     i = i + 1
     Do Until Regex.IsMatch("\d\d\d", s(i))
       r.NAME = r.NAME & " " & s(i)
       i = i + 1
     Loop
     r.FL = s(i) '111
     i = i + 1
     r.RK = s(i) 'MRR
     i = i + 1
     r.COP = s(i) 'YY2
     i = i + 1
     r.DEP = s(i) ' 05-03-15
     i = i + 1
     r.TM = s(i) '1919
     i = i + 1
     If s(i).StartsWith("@") Then
       r.FLTNO = s(i)
       i = i + 1
     End If
     r.ARR = s(i)
     i = i + 1
     r.TM2 = s(i)
     i = i + 1   
     res.Add(r)   
   Next
   Return res
End Sub
 
Upvote 0

tcgoh

Active Member
Licensed User
Longtime User
Try it with this code:
B4X:
Sub Process_Globals
   Type Record (SN As String , NAME As String, FL As String, RK As String, COP As String _
     , DEP As String, TM As String,FLTNO As String, ARR As String, TM2 As String)
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     ParseList
   End If
End Sub

Sub ParseList As List
   Dim lines As List = File.ReadList(File.DirAssets, "all.txt")
   Dim res As List
   res.Initialize
   For Each line As String In lines
     Dim s() As String = Regex.Split("\s+", line)
     If s.Length < 10 Then Continue
     Dim r As Record
     r.Initialize
     Dim i As Int = 0
     r.SN = s(i)
     i = i + 1
     r.NAME = s(i)
     i = i + 1
     Do Until Regex.IsMatch("\d\d\d", s(i))
       r.NAME = r.NAME & " " & s(i)
       i = i + 1
     Loop
     r.FL = s(i) '111
     i = i + 1
     r.RK = s(i) 'MRR
     i = i + 1
     r.COP = s(i) 'YY2
     i = i + 1
     r.DEP = s(i) ' 05-03-15
     i = i + 1
     r.TM = s(i) '1919
     i = i + 1
     If s(i).StartsWith("@") Then
       r.FLTNO = s(i)
       i = i + 1
     End If
     r.ARR = s(i)
     i = i + 1
     r.TM2 = s(i)
     i = i + 1  
     res.Add(r)  
   Next
   Return res
End Sub
Hi Erel,

Thanks for the above codes and sorry for coming back to this after so long a break.
But how do i convert the records into SQL file. Sorry I have no knowledge of RECORD type.

Thanks
 
Upvote 0
Top