Android Question SOLVED SaveCSV error

elitevenkat

Active Member
Licensed User
Longtime User
B4X:
Public Sub ExportTableToCSV
    private Provider As FileProvider
    Private xui As XUI

    Dim data As List
    data.Initialize
    Dim cursor As Cursor = Starter.Sql.ExecQuery("SELECT * from excelexport")
'    cursor = Starter.sql.ExecQuery("SELECT * FROM agg where active=1")
 
'     cursor
 
    For i = 0 To cursor.RowCount - 1
        cursor.Position = i
         Dim row(cursor.ColumnCount) As String
        For k = 0 To cursor.ColumnCount - 1         
            row(k) = cursor.GetString2(k)
        Next
        data.Add(row)
    Next
    cursor.Close
    Provider.Initialize
    Dim su As StringUtils
'    xui.
    Dim cf As String="excelldata.csv"
    xui.SetDataFolder("feeto")
    su.SaveCSV(xui.DefaultFolder, cf, ",", data) ''error java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
 
'tried this also 
 su.SaveCSV(File.DirInternal,  cf , ",", data) '

'error java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
End Sub
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
Your code as written looks good, although it is preferable to use Resultset instead of cursor, but that has nothing to do with the error. The problem is elsewhere. Check your data and the size.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Clutching at straws here but
Not clutching at straws. The SQL library will return a Null value for NULL SQL values. This most likely is the issue and I think your solution is a good solution w/o testing first to see if the returned value is a (Java) null String. Note that @Erel is aware of this, but will not change the SQL library due to backward compatibility/consistency.

Update: @Erel's comment on this issue: https://www.b4x.com/android/forum/threads/null-not-evaluating-as-null.82458/post-522554
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The SQL library will return a Null value for NULL SQL values
row(k) = "" & cursor.GetString2(k)
I normally use something like this to substitute the NULL for an empty string:
B4X:
If cursor.GetString2(k) = Null Then
                row(k) =""
Else
                row(k) = cursor.GetString2(k)
End If
@emexes uses the following which is also correct: row(k) = "" & cursor.GetString2(k)
I wonder which ones causes more overhead or if one is preferable to the other.
 
Upvote 0

emexes

Expert
Licensed User
@emexes uses the following which is also correct: row(k) = "" & cursor.GetString2(k)
My mindset at the time was to make the smallest possible change to the code to try identify/resolve the problem, and using If x Is String Then or If x = Null Then meant either resolving x twice or adding a temporary variable.

This looks good:
B4X:
If cursor.GetString2(k) = Null Then
    row(k) =""
Else
    row(k) = cursor.GetString2(k)
End If
although my innate paranoia šŸ¤£ means I'd tend towards using a temporary variable to eliminate the duplicate call of cursor.GetString.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
My mindset at the time was to make the smallest possible change
Usually, the NULL problem may be only affecting one column or so, so what about something like this:. I think it would make your code even more efficient and you are not altering every data point. What say you?
B4X:
       For k = 0 To cursor.ColumnCount - 1  
            If k=1 Then
                row(k) = "" & cursor.GetString2(k)
            Else
                row(k) = cursor.GetString2(k)
            End If        
        Next
 
Upvote 0
Top