Android Question How to replace null values with zero's in double array?

RB Smissaert

Well-Known Member
Licensed User
Longtime User
This sounds like a simple thing, but not managed it so far.
What would be the best way to do this?

RBS
 

stevel05

Expert
Licensed User
Longtime User
How are you creating the double array?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you can't change the source to exclude nulls, then I think you will have to cast the array initially to an array of object to do the conversion, then create the Double array, replacing the nulls as you go.

B4X:
'ObjArr is an array of Object read from the database
Dim DblArr(ObjArr.Length) As Double
For i = 0 To ObjArr.Length - 1
    Dim O As Object = ObjArr(i)
    If O = Null Then
        DblArr(i) = 0
    Else
        DblArr(i) = O
    End If
Next
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Sure, it's more universal.
But, it seems that he expects a Double therefore the 'limited' code works.
Because, I hope so, the type check was done before.
I usually do not argue with klaus. Shouldn't this be:
B4X:
If str <> Null and isnumber(str) Then
I also normally don't contradict @klaus, but I think this time I disagree with his solution.
If you use this statement str = Curs.GetString2(col) and the value is null str does not become null but sometimes str = "null" as a string, I would do so

B4X:
str = Curs.GetString2(col)
If str <> Null And str<>"null" Then
     If isnumber(str)  Then
     End IF
End IF
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I also normally contradict @klaus, but I think this time I disagree with his solution.
If you use this statement str = Curs.GetString2(col) and the value is null str does not become null but sometimes str = "null" as a string, I would do so

B4X:
str = Curs.GetString2(col)
If str <> Null And str<>"null" Then
     If isnumber(str)  Then
     End IF
End IF

The check can be done in a single line:
B4X:
str = Curs.GetString2(col)
If str <> Null And str <> "null" And IsNumber(str) Then
'
End IF
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
The check can be done in a single line:
B4X:
str = Curs.GetString2(col)
If str <> Null And str <> "null" And IsNumber(str) Then
'
End IF
I'm not sure that isNumber with a null value doesn't generate a runtime error
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
B4X short circuits logical evaluations. The first false expression in an and expression stops checking and therefore does not evaluate any other expressions in the statement.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Since the OP retrieves data from a SQLite table, shouldn't he try to avoid the presence of a null in any of his output by simply invoking the Ifnull function like this or am I slow to catch up to this debate:
B4X:
ifnull(mycol, 0) as col
 
Last edited:
Upvote 0
Top