NilObjectException on line "rs = DB.SQLSelect(sql)"

Hi All,

So I’m trying to get my WebService App to send a reply to a client App as JSON.

I am able to boot both applications (The WebService and Client Test App) fine however once I press the button in the test app I get a “NilObjectException” Error in the Web Service App on the line in Bold Italics below:

	Dim sql As String
	sql = "SELECT * FROM Contacts"
	
	Dim rs As RecordSet
	[b][i]rs = DB.SQLSelect(sql)[/i][/b]
	
	If DB.Error Then
			System.DebugLog("DB Error: " + DB.ErrorMessage)
			
			Dim jsonError As New JSONItem
			jsonError.Value("DBError") = DB.ErrorMessage
	End If
	
	Dim jsonContacts As New JSONItem
	Dim cont As New Dictionary
	
	While Not rs.EOF
			cont = New Dictionary
			cont.Value("fullname") = rs.Field("fullname").StringValue
			
			jsonContacts.Value(rs.Field("fullname").StringValue) = cont
			
			rs.MoveNext
	Wend
	
	rs.Close
	
	Dim jsonResults As New JSONItem
	jsonResults.Value("GetAllContacts") = jsonContacts

Apologies if I have posted this in the wrong section, feel free to move the post should it belong elsewhere :slight_smile:

Dim rs As RecordSet

rs = DB.SQLSelect(sql)

So DB is Nil ?

Check against Nil before that line.

Also, you can check RS against Nil after that line.

RS will only be nil in one of two major situations.

a) DB does not have a valid active connection
b) the SQL is not valid (RS would not be nil if SQL is valid, but no records returned)

And since the SQL is rather simple in this case, I’m guessing it is “A”

Hi Guys,

Thanks for your replies, the Line before the Nil is below in bold:

	Dim data As Text = DefineEncoding(Request.Entity, Encodings.UTF8).ToText
	Dim json As JSONItem
	
	Select Case Request.Path // /GetAllContacts
	Case "GetAllContacts"
			json = [b]GetAllContacts[/b]
			Request.Status = 200
			
	Case "GetContact"
			json = GetContact
			Request.Status = 200
			
	Case Else
			// Do not process request
			Return False
	End Select
	
	Request.Print(json.ToString)
	Return True

Also I have this Event Handler which would bring up an error message if the DB wasn’t connected (the DB is in the folder as the project):

	Dim dbFile As FolderItem
	dbFile = GetFolderItem("edata.db")
	
	If Not dbFile.Exists Then
			System.DebugLog("DB not found: " + dbFile.NativePath)
			Quit
	End If
	
	DB = New SQLiteDatabase
	DB.DatabaseFile = dbFile
	
	If Not DB.Connect Then
			System.DebugLog("Could not connect to database: " + DB.ErrorMessage)
			Quit
	End If
	
	System.DebugLog("Connect to database: " + dbFile.NativePath)

first it is a database issue, now a JSON one?
what is GETCONTACTS

If you say so… good luck…can’t help without detailed information

DB is nil
This means you either didnt

Dim DB as new Databaseofsomekind

before this line happens.

Or that it doesnt have a valid active connection to your database.
Where do you set DB up?

thats is what I tried to tell him, but seems it just passed by… can’t work with incomplete and/or conflicting information… too early in the morning for that :slight_smile:

Was not being rude… you provided incomplete and conflicting information, and it is an investement of time for us to help you.
You have twice been given the probable cause,