Android Question How to replace the null value of a label

Daniel44

Active Member
Licensed User
Hey everyone!

Im storing inside a map some double values... but when that values inside the map are null and I want to show in a label.text = 0,00 it shows me null... I mean If my map.size = 0 I need to show 0,00 on the label.text and I can't change that text

Here's my map

B4X:
account = parser.NextArray
    For i = 0 To account.Size - 1
    Dim m As Map
    m = account.Get(i)
If m.Size = 0 Then
LblValues.Text = "0,00 "

on my my devece lblValues shows me: null and I need it shows me 0,00

I've changed it to '00,0' then to 0 then to '0' I also created a varible to assign to the label with a 0,00 value but nothing happens.. it just still showing null. Thank you
 

OliverA

Expert
Licensed User
Longtime User
Where is the code after if m.size=0?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
When you say, showing null do you mean nothing or the letters n u l l?
 
Upvote 0

Daniel44

Active Member
Licensed User
When you say, showing null do you mean nothing or the letters n u l l?

The text of my label says: null

when my map has values it shows me that values correctly for ex. 129,99.. but when there's no values... my label.text shows me the word null
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Did you try something like this to force it to enter the zeros:
B4X:
If m.Size = 0 Then
        If LblValues.Text =Null Then
            LblValues.Text = "0,00 "
        End If
End If
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
when my map has values it shows me that values correctly for ex. 129,99.. but when there's no values... my label.text shows me the word null
I have no more code ... just that
This is confusing. I asked you if there is more code and you said no. Nowhere do I see how you set you label if the map size is not 0. You are showing too little code. Plus, you need to log the size of the map and see if it is ever zero. My guess: it is not. Proof me wrong with some logging. And show me the code on how your label gets your example 129,99
 
Upvote 0

Daniel44

Active Member
Licensed User
Hey Oliver you're right there's more code but it's before this... you asked after this one...this code is part of a jobDone and it's based on a mysql stencence which feed that map Next this just there's an end if.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Post the whole jobdone method
 
Upvote 0

Daniel44

Active Member
Licensed User
This is confusing. I asked you if there is more code and you said no. Nowhere do I see how you set you label if the map size is not 0. You are showing too little code. Plus, you need to log the size of the map and see if it is ever zero. My guess: it is not. Proof me wrong with some logging. And show me the code on how your label gets your example 129,99

B4X:
Por favor, actualiza a B4A-Bridge v2.30+
Registo conectado a:  xxxxx xx-xxxM-xxxxx111xxxx
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (est) Create, isFirst = true **
** Activity (est) Resume **
Response from server: [{"TotalesC":"129.00"}]
Response from server: [{"TotalesF":null}]
(MyMap) {TotalesF=null}
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You see, the servers response is an array with a map that has one item in it (size = 1, not 0)! That item happens to have a null value and therefore your label is/was (in case you changed your code) showing null.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
B4X:
    Dim parser As JSONParser
   parser.Initialize($"[{"TotalesF":Null}]"$)
   Dim root As List = parser.NextArray
   For Each colroot As Map In root
       Dim TotalesF As String = colroot.Get("TotalesF")
       If (TotalesF = "null") Then  LblValues.Text = "0,00 " Else  LblValues.Text = TotalesF
   Next

Edit: Oliver was quicker than me :)
 
Upvote 0

Daniel44

Active Member
Licensed User
You see, the servers response is an array with a map that has one item in it (size = 1, not 0)! That item happens to have a null value and therefore your label is/was (in case you changed your code) showing null.

Yes Oliver but it shows null because into the mysql table there's no values to sum, but when there are values it gives me the sum of all items. Thank you
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Yes, but the null is assigned to your text field and then it shows null. If you would finally show me the code where you set that field, I could help you out. Hint: a string that has a “null” value actually contains the word “null” (lower cased and without quotes).
 
Upvote 0

Daniel44

Active Member
Licensed User
B4X:
    Dim parser As JSONParser
   parser.Initialize($"[{"TotalesF":Null}]"$)
   Dim root As List = parser.NextArray
   For Each colroot As Map In root
       Dim TotalesF As String = colroot.Get("TotalesF")
       If (TotalesF = "null") Then  LblValues.Text = "0,00 " Else  LblValues.Text = TotalesF
   Next

Edit: Oliver was quicker than me :)

Hey udg thank you! I'll try what you're posting here. As Oliver says (if I haven't missunderstood) I have to work with the item directly inside the map not with its size.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
As Oliver says (if I haven't missunderstood) I have to work with the item directly inside the map not with its size.
Yes! :D
 
Upvote 0

Daniel44

Active Member
Licensed User
Here's my jobdone
B4X:
Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
        Dim res As String
        Dim jobResult As String = Job.GetString
        res = Job.GetString2("UTF8")
'        res = res.SubString(1)
        Log("Response from server: " & res)
        Dim parser As JSONParser
        parser.Initialize(res)
        Select Job.JobName
            Case TRAERTOTALFIADOS
                Dim montofiado As List = Json2List(jobResult)
                
                montofiado = parser.NextArray 'returns a list with maps
                For i = 0 To montofiado.Size - 1
                    Dim m As Map
                    m = montofiado.Get(i)
                    Log (m)
'                   
                    If m.Size = 0 Then
                        Dim TotalesF As String = m.Get("TotalesF")
                        If (TotalesF = Null) Then
                            lblTotaFiado.Text = "0.00"
                        End If
                                        
                        
                    Else
'                        Dim m As Map
'                        m = l.Get(0)
                        lblTotaFiado.Text = (m.Get("TotalesF"))
                    End If
                Next
            Case TRAERTOTALCASH
                Dim l As List = Json2List(jobResult)
                l = parser.NextArray
                If l.Size = 0 Then
                    totalCash.Text = 0
                    
                Else
                    Dim m As Map
                    m = l.Get(0)
                    totalCash.Text = (m.Get("TotalesC"))
                End If
            Case GRIDFIADOS
                Dim listagrid As List = Json2List(jobResult)
                listagrid = parser.NextArray
                For i = 0 To listagrid.Size - 1
                Dim m As Map
                m = listagrid.Get(i)
                Log (m)
                
                Dim m As Map
                m =  listagrid.Get(0)
                Next
'                Dim g As List = Json2List(jobResult)
'                Log(g)
'                g = parser.NextArray
'                Log(g)
'                If g.Size = 0 Then
'                    Else
'                    Dim m As Map
'                    m = g.Get(0)
                'MostrarGridFiado(m)           
            '    End If
        End Select
    Else
        Log(Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

and it still showing my lbltotafiado.text null

here's the log
B4X:
--------- beginning of main
** Activity (est) Create, isFirst = true **
** Activity (est) Resume **
Response from server: []
Response from server: [{"TotalesC":"62112.55"}]
Response from server: [{"TotalesF":null}]
(MyMap) {TotalesF=null}
** Service (starter) Destroy (ignored)**
** Activity (est) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (est) Create, isFirst = true **
** Activity (est) Resume **
Response from server: [{"TotalesC":"62112.55"}]
Response from server: []
Response from server: [{"TotalesF":null}]
(MyMap) {TotalesF=null}
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
If m.Size = 0 Then
Dim TotalesF As String = m.Get("TotalesF")
If (TotalesF = Null) Then
lblTotaFiado.Text = "0.00"
End If


Else
' Dim m As Map
' m = l.Get(0)

lblTotaFiado.Text = (m.Get("TotalesF"))
End If
You don’t need the if/else. Only the 4 lines in the if portion. Get rid of of the rest (of what is quoted)
 
Upvote 0
Top