Map not Initialized on return of Dbutils.ExecuteMap

jtaj

Member
Licensed User
Longtime User
The following code continues to give me a "RUntime Exception: object should first be initialized (map)". The results of the query I perform are NULL (as there is no record that matches. I understood that the QueryResults Map would then have a NULL in it.

What am I doing wrong, I've looked at this 10 ways to Sunday?


B4X:
Sub FindSalebyUPC(whatupc As String)
   Dim sqlcmd As String
   Dim QueryResults As Map
   Log("Sales:FindSalebyUPC: Searching for " & whatupc)
   
   sqlcmd = "select * from items_sold, sales where sales.id = sales_id AND upc ='" & whatupc &"'"
   QueryResults = DBUtils.ExecuteMap(Main.SQL,sqlcmd,Null)
   If QueryResults = Null Then
      Msgbox("No sale found for that UPC", "E R R O R")
   Else
      lblSalesId.Text = QueryResults.Get("sales_id")
      lblDtTm.Text = QueryResults.Get("date")
      edtTender.Text = QueryResults.Get("amttender")
      lblPaidby.Text = QueryResults.Get("paidby")
      edtUpc.requestfocus()
   End If
   Return
   
End Sub
 

jtaj

Member
Licensed User
Longtime User
Erel,
Not at all. I'm sure you get these dumb newbie questions all the time and God bless you for respond to us all. :)

I still have not completely got when and where I need to initialize things in B4a. I want to say in my quest I tried this, but I will look again. I'm sure that is the problem, the initializing step is tripping me quite a bit. I'm switching between VB on Access, PHP on Linux and this.

Thanks for replying
Teresa
:sign0104:
 
Upvote 0

jtaj

Member
Licensed User
Longtime User
Erel,
I added an initialize and it does in fact initialize the QueryResults map. But then after it returns from the DBUtils function, the QueryResults is back to not initialized (I see this in the debugger).

I'm attaching the DBUtils ExecuteMap code I'm using in case I messed it up.

I'm am inputting an unfindable upc # so that the ExecuteMap is supposed to return a Null value.


B4X:
Sub FindSalebyUPC(whatupc As String)
   Dim sqlcmd As String
   Dim QueryResults As Map
   Log("Sales:FindSalebyUPC: Searching for " & whatupc)
   
   If (QueryResults.IsInitialized)= False Then
      QueryResults.Initialize
   End If
   sqlcmd = "select * from items_sold, sales where sales.id = sales_id AND upc ='" & whatupc &"'"
   QueryResults = DBUtils.ExecuteMap(Main.SQL,sqlcmd,Null)
   If QueryResults = Null Then
      Msgbox("No sale found for that UPC", "E R R O R")
   Else
      lblSalesId.Text = QueryResults.Get("sales_id")
      lblDtTm.Text = QueryResults.Get("date")
      ShopCartLoad
      edtTender.Text = QueryResults.Get("amttender")
      lblPaidby.Text = QueryResults.Get("paidby")
      edtUpc.requestfocus()
   End If
   Return
   
End Sub

B4X:
Sub ExecuteMap(SQL As SQL, Query As String, StringArgs() As String) As Map
   Dim cur As Cursor
   If StringArgs <> Null Then 
      cur = SQL.ExecQuery2(Query, StringArgs)
   Else
      cur = SQL.ExecQuery(Query)
   End If
   Log("DBUtils:ExecuteMap: " & Query)
   If cur.RowCount = 0 Then
      Log("DBUtils:ExecuteMap: No records found.")
      Return Null
   End If
   Dim res As Map
   res.Initialize
   cur.Position = 0
   For i = 0 To cur.ColumnCount - 1
      Log("DBUtils:ExecuteMap: Results(" & i & "):" & cur.GetColumnName(i) & "=" & cur.GetString2(i))
      If cur.GetString2(i) = Null Then
         Log ("DBUtils:ExecuteMap: value is null")
      End If
      res.Put(cur.GetColumnName(i).ToLowerCase, cur.GetString2(i))
   Next
   cur.Close
   Return res
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm afraid that you didn't understand my (too) short answer.

ExecuteMap returns an uninitialized map if no records were found. This is not something general to Basic4android. It is just the way that this sub works. So in your code you should change:
B4X:
If QueryResults = Null Then
To:
B4X:
If QueryResults.IsInitialized = False Then

Normally objects in Basic4android are never Null. Instead they are not initialized.

Note that you do not need to initialize QueryResults yourself. It will just waste a few CPU cycled.
 
Upvote 0

jtaj

Member
Licensed User
Longtime User
Thank you Erel. I had given up. I understand now, thank you for the quick lesson. It's doing what it is supposed to now.
 
Upvote 0

s0m4y3h

New Member
hello please help me

hello
i am from iran
you can help me for desing the persian programs for android
eclips and basic4
___how instal plugin new project android in eclips
 
Upvote 0
Top