Android Question MSSQL Boolean problem

Tomas Petrus

Active Member
Licensed User
Longtime User
Hi,

I am using this tutorial with HttpUtils2 and everything is working perfectly, but I have problem with bool variable.
I have this code:

B4X:
Dim Start = False As Boolean

If  m.Get("Start") = Null Then
      Start = False
Else
     Start = m.Get("Start")
End If

Log(Start)      
If Start = True Then 
     'Do something
  Else
     'Do something else
End If


Sometimes I get NullPointerException in "If Start = True Then", but it should not be possible and in log is "false" so variable isn't Null. It is really wierd, because it does not happen everytime.

Thanks for Answer!
 

Tomas Petrus

Active Member
Licensed User
Longtime User
Thanks for answer, but problem is somewhere else. "m.Get("Start")" isn't null. I am pretty sure. It cannot be and I need to read that value.
This part is there, just because I couldn't find problem
B4X:
If m.Get("Start") = Null Then
Start = False
Else
Start = m.Get("Start")
EndIf

Default value for that is False by the way, I need to check if it's True. I still sometimes get NullPointerException on "If Start = True Then"
 
Upvote 0

Reviewnow

Active Member
Licensed User
Longtime User
if your msSql column is a bit column
msSql does not return True or False it returns 1 or 0

check for null values
select * from my table where myfield is null or myfield = ''
update myfield set myfield = 0 where myfield is null or myfield = ''
go to design view of the table set a default value of 0 or 1 on the bit column


Else if it is a varchar field
just run this statement on the server
update myfield set my field = 'False' where myfield is null or myfield = ''
go to design view of the table set False or True as a default value of the column
 
Last edited:
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
Thanks for answer, but you don't have truth. I changed my code to this

B4X:
Log("Start - DB: " & m.Get("Start"))
         
       If m.Get("Start") = 1 Then
             Start = True
          Else
               Start = False
          End If
         End If
        
    Log(Start)

If Start = True Then
'Do something
Else
'Do something else
EndIf

and in log i get :
Start - DB: true
false

so it isn't 1 or 0 but true/false. I still get NullPointerException on "If Start = True Then" and i fill it just with False / True. How is it possible?

Thanks
 
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
hi, there it is:

Error occurred on line: 425 (main)
java.lang.NullPointerException
at anywheresoftware.b4a.samples.sqlserver.main._jobdone(main.java:1303)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:305)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA$3.run(BA.java:319)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)


Thanks!
 
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
it is long, but there it is:

B4X:
Case "LoadNeprirazeneZakazky"
  response = Job.GetString()
         parser.Initialize(response)
         rows = parser.NextArray
         
         If rows.Size > 0 Then
           ZpravyHeight = 140
         End If
     
       For i = 0 To rows.Size - 1
           
                     
           If i = 0 Then
           
             Dim GD As GradientDrawable
             GD.Initialize("TOP_BOTTOM", Array As Int(Colors.ARGB(255, 93,153,195), Colors.ARGB(255, 58,108,159)))
             
             NadpisZakazky.Text = "  Zakázky"
             NadpisZakazky.Background = GD
             NadpisZakazky.TextColor = Colors.White
             NadpisZakazky.TextSize = 18
             
             
             Activity.AddView(NadpisZakazky, 0, ZpravyHeight, 100%x, 25dip)
           
           End If
           
           Dim m As Map
           m = rows.Get(i)
         
           
           Dim GetTon As HttpJob
           GetTon.Initialize("GetTon",Me)
           
           GetTon.PostString(ServerUrl, "SELECT * FROM Tons WHERE zakid="& m.Get("zakID")  &" AND ridid="& ridID &" AND bipTon=0")
  
           Dim Adresa As String
                     
           If m.Get("kluName") <> Null Then
             If m.Get("kluName") <> "" Then
                 Adresa = m.Get("kluName") & ", "
             End If
           End If
           
           Dim Start As Boolean = False
           If   m.Get("Start") = Null Then
           
             Start = False
           Else
           
             Log("Start - DB: " & m.Get("Start"))
           
             If m.Get("Start") = True Then
               Start = True
               Else
               Start = False
             End If
           End If
           
           
           Log(Start)
           If Start = True Then  'there is error
           
             Adresa =  Adresa  &m.Get("zakNastup")
             
           Else If m.Get("obec_kod") = "554782" Then
           
               Adresa = Adresa & m.Get("nazev")&" "
             
               If m.Get("ce") <> "" Then
                 Adresa = Adresa  & m.Get("ce") & "/"
                End If
               
               Adresa = Adresa &  m.Get("cp")
           Else   
                 If m.Get("ulice_kod")= "471461" Then
               
                 Adresa = Adresa & m.Get("zakMesto")&" "
             
                   If m.Get("ce") <> "" AND m.Get("ce") <> Null Then
                     Adresa = Adresa  & m.Get("ce") & "/"
                     
                   End If
                   
                     Adresa = Adresa & m.Get("cp")   
                 
        Else
                   
                 Adresa = Adresa & m.Get("nazev")&" "
             
                   If m.Get("ce") <> ""  AND m.Get("ce") <> Null Then
                     Adresa = Adresa & m.Get("ce") & "/"
                     
                   End If
                                   
                      Adresa = Adresa  & m.Get("cp")
                      
                   If m.Get("zakMesto") <> "" AND m.Get("zakMesto") <> Null Then   
                     Adresa = Adresa & " - " & m.Get("zakMesto")
                  End If  
                 
                End If  
            End If
                    
           
           Dim Specifikace As String
           
              If m.Get("zakCombi") = True Then
              Specifikace = " - combi"
              End If
             
              If m.Get("zakAng") = True Then
              If Specifikace <> "" Then
                  Specifikace =  Specifikace & ", anglicky hovořící"
              Else
                  Specifikace = " - anglicky hovořící"
              End If
              End If
             
              If m.Get("zakRus") = True Then
              If Specifikace <> "" Then
                  Specifikace =  Specifikace & ", rusky hovořící"
              Else
                  Specifikace = " - rusky hovořící"
              End If
              End If
             
              If m.Get("zakPes") = True Then
              If Specifikace <> "" Then
                  Specifikace =  Specifikace & ", pes"
              Else
                  Specifikace = " - pes"
              End If
              End If
             
              If m.Get("zak5Osob") = True Then
              If Specifikace <> "" Then
                  Specifikace =  Specifikace & ", 5 osob"
              Else
                  Specifikace = " - 5 osob"
              End If
              End If
             
              If m.Get("zak6Osob") = True Then
              If Specifikace <> "" Then
                  Specifikace =  Specifikace & ", 6 osob"
              Else
                  Specifikace = " - 6 osob"
              End If
              End If
             
              If m.Get("zakCarNumber") <> "" Then
              If Specifikace <> "" Then
                  Specifikace =  Specifikace & ", řidič: " & m.Get("zakCarNumber")
              Else
                  Specifikace = " - řidič: " & m.Get("zakCarNumber")
              End If
              End If
           
           Zakazky.SingleLineLayout.Label.TextSize = 14
           Zakazky.SingleLineLayout.Label.TextColor = Colors.Black
           Zakazky.AddSingleLine2(Adresa & " " & Specifikace ,m.Get("zakID"))   
           
           ZakazkyHeight = ZakazkyHeight + 200
           
       Next
       
       Try
         Dim GD As GradientDrawable
        GD.Initialize("TR_BL", Array As Int(Colors.White, Colors.White))
        Zakazky.ScrollingBackgroundColor = Colors.Transparent
         Zakazky.Background = GD
       
         Activity.AddView(Zakazky, 0, ZpravyHeight + 25dip, 100%x, ZakazkyHeight)
       Catch
         
         Log("Chyba vypisu")
         
       End Try

thanks!
 
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
yes

B4X:
Sub JobDone (Job As HttpJob)

  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
 
     Dim parser As JSONParser
     Dim response As String
     
     Dim rows As List

 If Job.Success = True Then
    Select Job.JobName
     Case"LoadNeprirazeneZakazky"
       
       'previous code
   
     End Select
  Else
     Log("Error: " & Job.ErrorMessage)
     ToastMessageShow("Error: " & Job.ErrorMessage, True)
  End If
  Job.Release

End Sub

m isn't Null. It does not happen every time in same select as I wrote in first post.

Thanks!
 
Upvote 0

Reviewnow

Active Member
Licensed User
Longtime User
I think I see the issue, m.get("Start") is actually returning a string value not boolean

If m.Get("Start") = "True" Then
Start = True
Else
Start = False
End If
 
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
I've tried it without change. Problem is really weird. I've tried this:

B4X:
Dim Start As Boolean = False
         '   If  m.Get("Start ") = Null Then
           '   Start = False
           'Else
             'Log("Start - DB: " & m.Get("Start"))
           
             'If m.Get("Start") = "True" Then
             '   Start = True
               'Else
               'Start = False
             'End If
           'End If
           
           
           Log(Start)
           If Start = True Then  
           
           Else

           End If

and there is still NullPointerException on "If Start = True Then". How is it possible?
Thanks
 
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
now I tried this:

B4X:
     Dim Start As Boolean
     Start = False

     If m.Get("Start") = True Then
              Start = True
         Else
              Start = False
      End If

     Log(Start)
     If Start = True Then

      Else

     End If

It is working so far! So issue is this ?

B4X:
Dim Start As Boolean = False
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…