Android Question Empty String Issue

Luis Felipe Andrade

Member
Licensed User
Hello please, how to do this?
if clvLabel4.Text has a value then works ok but if clvLabel4.Text has no value, then I have Empty String Error
Null does not work here becouse clvLabel4.Text has no Null value, is just Empty.
* the error occurs in the line if
thank you in advance!

Empty String Issue:
'mapatarifas.Get("CP") has no value on database
clvLabel4.text= mapatarifas.Get("CP")

'this give Emprty String Error
 If clvLabel4.text<>""  Then
        clvLabel5.Text= NumberFormat2(clvLabel4.Text/ptc,0,2,2,False)
 End If
 
 'this also empty string error
 Dim laca As Double=clvLabel4.Text
 If laca>0   Then
    clvLabel5.Text= NumberFormat2(clvLabel4.Text/ptc,0,2,2,False)
End If
 
Solution
ChatGPT says..

In B4A (Basic4android), dealing with empty strings requires a bit of precaution, especially when you're performing operations like division or other arithmetic that can throw errors if the input isn't what's expected. Your issue seems to stem from attempting to perform an operation on an empty string as if it were a number, which indeed leads to errors. Let's address this with a more defensive programming approach.

You can solve this issue by checking if clvLabel4.Text is not only different from an empty string but also if it can be parsed to a number successfully before attempting any arithmetic operation on it. This can be achieved using the IsNumber function to check if the string represents a numeric value...

JohnC

Expert
Licensed User
Longtime User
ChatGPT says..

In B4A (Basic4android), dealing with empty strings requires a bit of precaution, especially when you're performing operations like division or other arithmetic that can throw errors if the input isn't what's expected. Your issue seems to stem from attempting to perform an operation on an empty string as if it were a number, which indeed leads to errors. Let's address this with a more defensive programming approach.

You can solve this issue by checking if clvLabel4.Text is not only different from an empty string but also if it can be parsed to a number successfully before attempting any arithmetic operation on it. This can be achieved using the IsNumber function to check if the string represents a numeric value. Here’s how you can refactor your code:

B4X:
' Ensure that clvLabel4.text has a value and that it's a numeric value
If clvLabel4.Text <> "" And IsNumber(clvLabel4.Text) Then
    ' Parse the text to a Double before doing the division
    Dim laca As Double = clvLabel4.Text
    If laca > 0 Then
        ' Since laca is already a Double, use it directly
        clvLabel5.Text = NumberFormat2(laca / ptc, 0, 2, 2, False)
    End If
Else
    ' Handle cases where clvLabel4.Text is not a number or is empty
    ' You might want to set clvLabel5.Text to some default value or leave it as it is
End If
Let’s break down the solution:
  1. Check if Text is Numeric: Before performing any operation, it checks if clvLabel4.Text is a number by using IsNumber. This is crucial because attempting to convert a non-numeric string to a number will lead to errors.
  2. Use Parsed Value: It uses the parsed laca variable directly in the operation, ensuring that the operation is performed on a numeric value.
By incorporating these checks, your code becomes more robust, effectively handling cases where clvLabel4.Text might be an empty string or not a valid numeric value. This approach prevents the "Empty String Error" by ensuring that arithmetic operations are only attempted on valid inputs.
 
Upvote 0
Solution

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Perhaps you could consider using getdefault instead of get on line 2 as well.

This means that clvLabel4.text defaults to an empty string if null.
Incorporating this in @JohnC solution gives.


B4X:
'mapatarifas.Get("CP") has no value on database
'if CP has no value then set to empty string
clvLabel4.text= mapatarifas.Getdefault("CP","")

' Ensure that clvLabel4.text has a value and that it's a numeric value
If clvLabel4.Text <> "" And IsNumber(clvLabel4.Text) Then
    ' Parse the text to a Double before doing the division
    Dim laca As Double = clvLabel4.Text
    If laca > 0 Then
        ' Since laca is already a Double, use it directly
        clvLabel5.Text = NumberFormat2(laca / ptc, 0, 2, 2, False)
    End If
Else
    ' Handle cases where clvLabel4.Text is not a number or is empty
    ' You might want to set clvLabel5.Text to some default value or leave it as it is
End If
 
 'this also empty string error
'remove these lines
' Dim laca As Double=clvLabel4.Text
' If laca>0   Then
'    clvLabel5.Text= NumberFormat2(clvLabel4.Text/ptc,0,2,2,False)
'End If
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
mapatarifas is Map
B4X:
clvLabel4.Text = IIf(IsNumber(mapatarifas.Get("CP")), mapatarifas.Get("CP"), 0)
clvLabel5.Text = IIf(clvLabel4.Text.As(Double) > 0, NumberFormat2(clvLabel4.Text.As(Double) / ptc, 0, 2, 2, False), 0)
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Yes, that is correct.

1712133522067.png

I realise that this may not be the right way to do it as, CP does exist, it is just Null.

So your way will work.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Hm, a label has a string as a text item. Luckily for us, a space appears as a void. So why not just fill the "empty" label with one space character? Then you test for one space character and stay away from the null and empty string issues
 
Upvote 0

Luis Felipe Andrade

Member
Licensed User
?
mapatarifas is Map
B4X:
clvLabel4.Text = IIf(IsNumber(mapatarifas.Get("CP")), mapatarifas.Get("CP"), 0)
clvLabel5.Text = IIf(clvLabel4.Text.As(Double) > 0, NumberFormat2(clvLabel4.Text.As(Double) / ptc, 0, 2, 2, False), 0)
this works perfect! also looks very nice, lol, after this operation I've to return the text to empty string again becouse in te UI no need to show 0 value.
Thank you!!
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
this works perfect! also looks very nice, lol, after this operation I've to return the text to empty string again becouse in te UI no need to show 0 value.
Thank you!!
B4X:
    clvLabel4.Text = IIf(IsNumber(mapatarifas.Get("CP")), mapatarifas.Get("CP"), "")
    clvLabel5.Text = IIf(IsNumber(clvLabel4.Text), IIf(clvLabel4.Text.As(Double) > 0,NumberFormat2(clvLabel4.Text.As(Double) / ptc, 0, 2, 2, False), ""), "")
 
Upvote 0
Top