B4J Question B4J sqlite SUM(column) : java.lang.NullPointerException: Cannot invoke "String.trim()" because "in" is null

DALB

Active Member
Licensed User
Hello everyone.

Just a thing:

I wish to do a SUM of amounts in a table (column named 'montant':
With this code...
sum of the column 'montant':
Dim va As Double=sql.ExecQuerySingleResult( _
'        "SELECT SUM(montant) FROM depenses WHERE idlieu = " & f )

where f is an id in a list ( 'Dim f As Int=li.Get(t)')

appears this message ...
B4X:
java.lang.NullPointerException: Cannot invoke "String.trim()" because "in" is null

BUT, with this code which is the same operation...
creating sums for each item in li (list of cities):
For u=0 To li.Size-1
        cursor=sql.ExecQuery( _
        "SELECT montant FROM depenses WHERE idlieu=" & li.Get(u))
        montants(u)=0
        Do While cursor.nextrow
            montants(u)=montants(u)+cursor.GetDouble("montant")
        Loop
        montants(u)=Round2(montants(u),2)
    Next

where 'li' is a list containing the id of the table's cities and where I make a reading line by line not using sqlite SUM.

It works perfectly.

I don't think that it's a problem of writing my values.
What about this ? I have read other lines on the web, but I didn't find any answer for this, even if I found a solution.
 

teddybear

Well-Known Member
Licensed User
sum of the column 'montant''montant':
Dim va As Double=sql.ExecQuerySingleResult( _
'        "SELECT SUM(montant) FROM depenses WHERE idlieu = " & f )
Why not using parameterized queries?
B4X:
sql.ExecQuerySingleResult2( "SELECT SUM(montant) FROM depenses WHERE idlieu =?", Array(f) )
'or
cursor=sql.ExecQuery2( _
        "SELECT montant FROM depenses WHERE idlieu=?",Array(li.Get(u)))
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Inside the For loop, check for the value of li.Get(t). It may returns Null value.
B4X:
For t = 0 To li.Size - 1
  Log("id=" & li.Get(t))
 
Upvote 0

DALB

Active Member
Licensed User
Hi ! Just a moment, I'll try these and come back to tell you what about ! Thanks for these points !
I' m busy !
 
Upvote 0

DALB

Active Member
Licensed User
Aeric and Teddybear, thanks for your answers.
Copying my pages in a new app, it works perfectly. I don't know why, amybe a bug somewhere !
 
Upvote 0
Top