Android Question saveCSV2 error

ProjectGroup19

Active Member
Licensed User
B4X:
Dim Data As List
    Data.Initialize
    Dim contact As List
    contact.Initialize
    
    
    Dim rs As ResultSet = Connection.DB.ExecQuery("SELECT MEMBERID,FNAME ||' '|| SNAME  AS NAME,DOB,CONTACT FROM MEMBERSHIP,ACCOUNT WHERE ACCOUNT.USERNAME=MEMBERSHIP.ACCOUNT")
    Do While rs.NextRow
    Dim row(5) As Object
    Dim    c(1) As String
        If DateUtils.GetMonthName( rs.GetString("DOB"))=lblMonth.Text Then
    
    row(0) = rs.GetString("MEMBERID")
    row(1) = rs.GetString("NAME")
    row(2) = DateTime.GetYear(DateTime.Now)-DateTime.GetYear(rs.GetString("DOB"))
    row(3) = rs.GetString("DOB")
    row(4) = rs.GetString("CONTACT")
    
            c(0)=rs.GetString("CONTACT")
        
    Data.Add(row)
    contact.AddAll(c)
    
        End If
    Loop
    rs.Close
    
    B4XTable1.SetData(Data)
    
    Log("Contacts: "&Array As String(contact))
    Dim su As StringUtils
    
    Dim folder As String = File.DirInternal
    If File.Exists(File.DirInternal, CSVFileName) Then
        folder = File.DirInternal
    Else
        File.Copy(File.DirAssets, CSVFileName, File.DirInternal, CSVFileName)
    End If
    
    su.SaveCSV2(folder, CSVFileName, "," , contact, headers)

Log Error:

Logger connected to: TECNO MOBILE LIMITED TECNO KB8
--------- beginning of crash
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (login) Create, isFirst = true **
** Activity (login) Resume **
** Activity (login) Pause, UserClosed = false **
** Activity (adashboard) Create, isFirst = true **
** Activity (adashboard) Resume **
** Activity (adashboard) Pause, UserClosed = false **
** Activity (birthday) Create, isFirst = true **
Error occurred on line: 40 (BIRTHDAY)
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.String[]
at anywheresoftware.b4a.objects.StringUtils.SaveCSV2(StringUtils.java:107)
at anywheresoftware.b4a.objects.StringUtils.SaveCSV(StringUtils.java:100)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
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 htu.cms.gec.birthday.afterFirstLayout(birthday.java:104)
at htu.cms.gec.birthday.access$000(birthday.java:17)
at htu.cms.gec.birthday$WaitForLayout.run(birthday.java:82)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6758)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
** Activity (birthday) Resume **
 

DonManfred

Expert
Licensed User
Longtime User
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.String[]
you are using a result and want to cast that result to a string. But it is a List, not a single string. You need to find out which value it is. Only you can know.
 
Last edited:
Upvote 0

ProjectGroup19

Active Member
Licensed User
you are using a result and want to cast that result to a string. But it is a List, not a single string. You need to find out which value it is. Only you can know.
Please, what should be the right way to do this; Retrieve the data from the query as a string and save to csv
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This line doesn't produce anything useful:
B4X:
Log("Contacts: "&Array As String(contact))

StringUtils.SaveCSV expects a list where each item of this list is an array of strings.

B4X:
contact.AddAll(c) 'wrong. You are expanding the array.
'best:
contact.Add(Array As String(rs.GetString("CONTACT")))
 
Upvote 0

ProjectGroup19

Active Member
Licensed User
This line doesn't produce anything useful:
B4X:
Log("Contacts: "&Array As String(contact))

StringUtils.SaveCSV expects a list where each item of this list is an array of strings.

B4X:
contact.AddAll(c) 'wrong. You are expanding the array.
'best:
contact.Add(Array As String(rs.GetString("CONTACT")))
Thank you
 
Upvote 0
Top