Android Question Checking for a Null value in a string?

Sanxion

Active Member
Licensed User
Longtime User
Guys

I am baffled as to the reason why this is not working:

B4X:
If MyNote <> Null And MyNote <> " " Then     
    etNote.Text = MyNote.Trim 
Else
    Msgbox("MyNote is null!","")
End If

Anybody know why the Null and empty string check is being ignored?

Thanks
 

klaus

Expert
Licensed User
Longtime User
What is not working ?
I suspect that this is wrong:
If MyNote <> Null And MyNote <> " " Then
I would use:
If MyNote <> Null And MyNote <> "" Then
In your code " " is the blank character and not an empty string.
 
Last edited:
Upvote 0

Sanxion

Active Member
Licensed User
Longtime User
What is not working ?
I suspect that this is wrong:
If MyNote <> NullAnd MyNote <> " " Then
I would use:
If MyNote <> NullAnd MyNote <> "" Then
In your code " " is the blank character and not an empty string.

I should have mentioned that at first I tried:

B4X:
If MyNote <> Null Then
       
        If MyNote <> "" Then   
           
            etNote.Text = MyNote.Trim   
       
        End If
    Else
       
        Msgbox("MyNote is null!","")
       
    End If

However, this line was still being executed: etNote.Text = MyNote.Trim

I therefore changed it to what I posted above and that has the same result. The point is that the first Null check does not work.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
The point is that the first Null check does not work.
Then, what is the etNote.text value? If for e.g. it is "null", which may happen if you're feeding from a database, then you should check for "null" instead of null.
 
Upvote 0

Sanxion

Active Member
Licensed User
Longtime User
Then, what is the etNote.text value? If for e.g. it is "null", which may happen if you're feeding from a database, then you should check for "null" instead of null.

etNote.text value is 'null' - see the attached file

Ok...I am now checking for all three scenarios...

B4X:
If MyNote <> Null Then
       
        If MyNote <> "" Then
           
            If MyNote <> "null" Then   
               
                etNote.Text = MyNote.Trim   
           
            End If
           
        End If
       
    Else
       
        Msgbox("MyNote is null!","")
       
    End If

The result is the same?
 

Attachments

  • MyNote.PNG
    311.1 KB · Views: 433
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
If you see "null" you should check for "null" not null, since I guess myNote is a string.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I saw your code, this is why I replied! Just remove the <>null check!
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
B4X:
if myNote="null" or myNote="" then
'doSomethingIfNeeded
log("null")
else
etNote.Text = MyNote.Trim 
end if
 
Upvote 0

Sanxion

Active Member
Licensed User
Longtime User
B4X:
if myNote="null" or myNote="" then
'doSomethingIfNeeded
log("null")
else
etNote.Text = MyNote.Trim
end if

Thank-you MC73..I tried that and received the same result.

The value is still null and the checks are ignored.
 
Upvote 0

Sanxion

Active Member
Licensed User
Longtime User
I have 'resolved' the issue by checking for a null in the returned SQL via a Sub level variable - which does work. I then add an empty space if it is a null.
 
Upvote 0

Mikonios

Active Member
Licensed User
Longtime User
Version 3.82
B4X:
Sub CheckNewConDat(MisValNew() AsString, MisValDat() AsString, i AsInt) AsString
Dim MiVal As String = ""
Dim MiResult As String = ""

If MisValNew (16) <> MisValDat (16) OR MisValNew (17) <> MisValDat (17) OR MisValNew (19) <> MisValDat (19) OR _
MisValNew (20) <> MisValDat (20) OR MisValNew (23) <> MisValDat (23) OR MisValNew (24) <> MisValDat (24) OR _
MisValNew (28) <> MisValDat (28) Then
...

When a single value is "null" ::
in "debug" mode it works perfectly.
in "release" always the same error:
B4X:
15/06/2016: 124432: pocess: lastexception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String.equals boolean (Object)' on a null object reference
Unable to resolve this problem
 
Last edited:
Upvote 0

Mikonios

Active Member
Licensed User
Longtime User
Solved with this inelegant modification that increases the response time on a DB with 10,000 records and 40 fields per record. ::::


B4X:
Sub CorrectNull(MyString As String) As String
    Try
        ' Force error
        If MyString.Length = 0 Then
            Dim MyNewStr As String = ""
        Else
            Dim MyNewStr As String = MyString
        End If
    Catch
        ' Capture the error
        Dim MyNewStr As String = ""
    End Try
    Return MyNewStr
End Sub

Sub CheckNewConDat(MisValNew() As String,  MisValDat() As String, i As Int) As String

    Dim MiVal As String = ""
    Dim MiResult As String = ""

    MisValNew(16) = CorrectNull(MisValNew(16)):        MisValDat(16) = CorrectNull(MisValDat(16))
    MisValNew(17) = CorrectNull(MisValNew(17)):        MisValDat(17) = CorrectNull(MisValDat(17))
    MisValNew(19) = CorrectNull(MisValNew(19)):        MisValDat(19) = CorrectNull(MisValDat(19))
    MisValNew(20) = CorrectNull(MisValNew(20)):        MisValDat(20) = CorrectNull(MisValDat(20))
    MisValNew(23) = CorrectNull(MisValNew(23)):        MisValDat(23) = CorrectNull(MisValDat(23))
    MisValNew(24) = CorrectNull(MisValNew(24)):        MisValDat(24) = CorrectNull(MisValDat(24))
    MisValNew(28) = CorrectNull(MisValNew(28)):        MisValDat(28) = CorrectNull(MisValDat(28))

    If MisValNew(16) <> MisValDat(16) OR MisValNew(17) <> MisValDat(17) OR MisValNew(19) <> MisValDat(19) OR _
        MisValNew(20) <> MisValDat(20) OR MisValNew(23) <> MisValDat(23) OR MisValNew(24) <> MisValDat(24) OR _
        MisValNew(28) <> MisValDat(28) Then
        MiResult = "UPDATE [10Datos] SET LstUpdte ='" &    MisValNew(13) & "', "

        MiResult = MiResult & "...='" &         MisValNew(16) & "', "
        MiResult = MiResult & "...='" &         MisValNew(17) & "', "
        MiResult = MiResult & "...='" &         MisValNew(19) & "', "
        MiResult = MiResult & "...='" &         MisValNew(20) & "', "
        MiResult = MiResult & "...='" &         MisValNew(23) & "', "
        MiResult = MiResult & "...='" &         MisValNew(24) & "', "
        MiResult = MiResult & "...='" &         MisValNew(28) & "' "

        MiResult = MiResult & "WHERE sLat = '" & MisValDat(0) & "' and sLng = '" & MisValDat(1) & "' "
        Try
            If Not(MisValNew(8) = Null) AND Not(MisValNew(8).Contains("'")) Then
                MiResult = MiResult & "AND Rotulo = '" & MisValNew(8) & "' "
            End If
        Catch
            LogMemory1("MiResult:" & i & "." & MiResult, "3")
        End Try

        MiResult = MiResult.Replace("=''", "=null")

    End If

    Return MiResult

'        LogMemory1("CheckNewConDat.lastexception: (" & i & ")." & LastException.Message, "3")

End Sub

Data Source :::
B4X:
    QryNew = "SELECT * FROM [" & TblNewName & "] WHERE sLat IS NOT NULL ORDER BY sLat"
    Dim LstResultNew As List
        LstResultNew = ExecuteMemoryTable(SQL1, QryNew, Null, 0)
    Dim MisValNew() As String = LstResultNew.Get(i)

Xample Image BBDD :::

 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…