I have an odd NilObjectException, too. The NOE occurs when I’m getting mailboxes from Mail while Mail has no account. I can reproduce the behaviour on another user. But I wasn’t able to get codesign working on the user. The app only hangs in this case. Instead I added some logging.
Below is part of the logging when getting the mailboxes doesn’t have a result but there is no NOE:
So the NOE happens between “TGetMailboxes.Run 2” and “GetImapStatus.Constructor”.
'extra logging
Globals.theErrorLog.logitem(currentMethodName + " 2")
'get mailboxes from IMAP or email client
dim loadForeignData as Boolean
Redim ImapStatus(-1)
dim MailboxDictionary as new Dictionary
if loadForeignData then
'some code for testing
else
dim theMailboxGetter as getAllMailfolders
if not isIMAP then
dim theGetter as new GetImapStatus(BundleID)
'extra logging
Globals.theErrorLog.logitem(currentMethodName + " 2a")
ImapStatus = theGetter.getAccounts
'extra logging
Globals.theErrorLog.logitem(currentMethodName + " 2b")
Which line of code between 2 and 2a could make an NOE?
have you try catch in this constructor? GetImapStatus
or what is the code in there?
log the beginning and end of each method.
use a variable “now i will do this”
and if you catch an error you can output or log this variable too. similar to row numbers.
add conditional compilings for bug hunting.
ImapStatus is a string array for holding information of accounts from an email client. I’m setting it in this line:
ImapStatus = theGetter.getAccounts
When the email client doesn’t have accounts then ImapStatus becomes nil. Unfortunately, arrays are treated something between an object and a simple value.
And, of course, all my email clients have accounts so this never happens for me.
I asked Norman because I was really confused too. Apparently this can happen when you don’t return an array from a function and use a variable to capture the result of the function.
I remember some years ago, perhaps 10 years, when this case was even worse. At that time, you couldn’t compare an array against nil (the compiler would complain the types didn’t match). There was no obvious way to check if the method correctly returned something other than handling a NilObjectException by trying to access the “array”.
Things have progressed
It’s a strongly typed language. I defined the return type as an array of data type. I would expect an empty array, not a nil object. Hence the ticket Norman created.
and fail to return an instance of MyClass, the function returns Nil. The behavior of a function that returns an array seems consistent with that.
If a function returns a primitive and you don’t return a value, you get the default value for the return type, but arrays are not a data type have no default “value”, so Nil make more sense to me.