Dim results As RecordSet = NilObjectException

Hi,
Dim results As RecordSet statement throws NilObjectException
can someone please tell whats wrong with this shared method?
I cant see it

  Dim sql As String
  sql = "SELECT DISTINCT artist FROM mp3Dat ORDER BY artist"
  
  Dim allArtist() As String
  Dim m As Integer
  Dim results As RecordSet
  results = MUSICDB.SQLSelect(sql)
  
  If results <> Nil Then
    While Not results.EOF
      For m = 0 to results.fieldcount-1
        allArtist.Append(results.IdxField(1).StringValue)
      next
      results.MoveNext
    Wend
    results.Close
  End If
  
  Return allArtist

Make sure you check for a database error after your query:

`
results = MUSICDB.SQLSelect(sql)
//add this:
 If MUSICDB.Error Then
    MsgBox("Error: " + Str(MUSICDB.ErrorCode) + " - " + MUSICDB.ErrorMessage)
  End If
//continue as before

Does adding this display a message?

Also what specific line displays the nil object exception in the debugger? A dim statement shouldn’t give you a nil object exception, so it must be another line… The more info you can provide the better the help we can give.

I think your query is only returning one field called artist. You can replace the entire m for loop with

allArtist.Append(results.Field("artist").StringValue)

@Jason King
tried the test for DB error - no message
as for specific line its the results = MUSICDB.SQLSelect(sql) statement
this seems to be an anomaly - I analyzed of the shared method via project menu
shows no problem.
@Tanner Lee tried your solution to no avail…

FYI this project work without problems via the carbon framework
these errors only occur using cocoa

I though I had this licked but I keep getting random errors
now I also have windows opening at startup that should be closed

MUSICDB is the only thing that could be nil on that line. Check the scope of your variables. One common error is to open the db with code like

dim musicdb as new xxxDatabase
...
musicdb.connect

This creates a temporary variable with the same name as the global one and uses it, leaving the global musicdb variable nil.

@Tim Hare
not sure I follow. So I had xojo analyze the datadase controller module
found Deprecated item RealSQLDatabase should be SQLiteDatabase instead
Maybe this is my problem?
I can find no example for the correct approach to make changes
here is the the printed Module. I think you may recognize the code.
its form a course I took a few years back
Help!

 

Project: mp3manager_OSXr21trial_v2


Date: Monday, December 8, 2014 11:27:51 AM



DatabaseController

Module DatabaseController


Private Const kDatabaseFilename = "MUSICDB.sqlite"



DatabaseController.ConnectToDatabase:

Protected Function ConnectToDatabase() As Boolean
MUSICDB = New REALSQLDatabase // SQLite

Dim dbFile As FolderItem
Dim dbFolder As FolderItem
dbFolder = SpecialFolder.ApplicationData.Child(App.kAppName)
If Not dbFolder.Exists Then
dbFolder.CreateAsFolder
End If

dbFile = dbFolder.Child(kDatabaseFilename)

MUSICDB.DatabaseFile = dbFile

Dim connect As Boolean
If dbFile.Exists Then
connect = MUSICDB.Connect
Else
// Create the database file
connect = MUSICDB.CreateDatabaseFile

If connect Then
// Create the DB Schema


connect = CreateDatabaseSchema
End If
End If

Return connect
End Function



DatabaseController.CreateDatabaseSchema:

Private Function CreateDatabaseSchema() As Boolean
MUSICDB.SQLExecute(MUSIC_SchemaSQL)

If Not IsError Then
MUSICDB.Commit
Return True
Else
Return False
End If
End Function



DatabaseController.DisplayDatabaseError:

Sub DisplayDatabaseError(doRollback as boolean)


MsgBox "Database Error: " + str(MUSICDB.ErrorCode) + EndOfLine + EndOfLine + MUSICD
B.ErrorMessage



// Rollback changes to the database if specified
if doRollback then
MUSICDB.rollback
end

End Sub



DatabaseController.IsError:

Protected Function IsError() As Boolean
If MUSICDB.Error Then
MsgBox("DB Error:" + Str(MUSICDB.ErrorCode) + " - " + MUSICDB.ErrorMessage)
Return True
Else
Return False
End If
End Function

MUSICDB As REALSQLDatabase



End Module

Again, musicdb is the only thing that could be nil on that line of code. Are you quite sure you called ConnectToDatabase before calling this code? Check the value of musicdb in the debugger.

@Tim Hare
I moved the If Not DatabaseController.ConnectToDatabase Then staement to top of the app open event
this seems to have corrected the problem.
Just wondering if the UnhandledException code I’am using is producing false positives
and may not be needed at?

  
  Dim name As String = Introspection.GetType(error).FullName
  
  System.DebugLog "Unhandled exception: " + name
  dim stack() as string = error.Stack
  for each frame As String in error.Stack
    if InStr(frame, ".") > 0 or InStr(frame, "%") > 0 then
      System.DebugLog "... " + NthField( frame, "%", 1  )
    end if
  next
  
  Dim mbox As New MessageDialog
  mbox.Title = "Unhandled " + name
  mbox.Message = "An unhandled " + name + " has occurred.  Please report this error to the developer."
  
  mbox.ActionButton.Caption = "Continue"
  mbox.AlternateActionButton.Caption = "Quit"
  mbox.AlternateActionButton.Visible = true
  if mbox.ShowModal = mbox.AlternateActionButton then
    Quit
    return false
  else
    return true
  end if

Also the my Toolbar buttons are very small and distorted is there a command to enlarge them?

TIA

No, it wouldn’t be able to produce a false positive. If you get to the UnhandledException event, you definitely have a problem.