# Odd NilObjectException

**URL:** <https://forum.xojo.com/t/odd-nilobjectexception/77444>\
**Category:** General\
**Created:** [September 28, 2023, 10:57am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444 "2023-09-28T10:57:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [September 28, 2023, 10:57am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/1 "2023-09-28T10:57:13Z")

</div>

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:

> 2023-09-28 12:47:27 CCEmailView.TGetMailboxes.Run accountname: Mail  
> 2023-09-28 12:47:27 CCEmailView.TGetMailboxes.Run 1  
> 2023-09-28 12:47:27 CCEmailView.TGetMailboxes.Run 2  
> 2023-09-28 12:47:27 GetImapStatus.Constructor  
> 2023-09-28 12:47:28 GetImapStatusMail.getAllAccounts accounts:  
> 2023-09-28 12:47:28 GetImapStatusMail.getAccounts no accounts found  
> 2023-09-28 12:47:28 GetImapStatus.Constructor done  
> 2023-09-28 12:47:28 CCEmailView.TGetMailboxes.Run 2a  
> 2023-09-28 12:47:28 CCEmailView.TGetMailboxes.Run 2b

When trying again I get the following in the log:

> 2023-09-28 12:47:33 CCEmailView.TGetMailboxes.Run accountname: Mail  
> 2023-09-28 12:47:33 CCEmailView.TGetMailboxes.Run 1  
> 2023-09-28 12:47:33 CCEmailView.TGetMailboxes.Run 2  
> 2023-09-28 12:47:33 --------------------------  
> 2023-09-28 12:47:33 An error happened:  
> 2023-09-28 12:47:33 Class/Method: CCEmailView.TGetMailboxes.Run  
> 2023-09-28 12:47:33 Time: 28. Sep 2023 12:47:33 60903313  
> 2023-09-28 12:47:33 Type of Error: NilObjectException  
> 2023-09-28 12:47:33 Error Number: 0  
> 2023-09-28 12:47:33 Error Message:  
> 2023-09-28 12:47:33 --------------------------  
> 2023-09-28 12:47:33 Stack:  
> 2023-09-28 12:47:33  
> Sub CCEmailView.CCEmailView.TGetMailboxes\_Run(CCEmailView.CCEmailView, Thread)

So the NOE happens between “TGetMailboxes.Run 2” and “GetImapStatus.Constructor”.

```auto
'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?

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [September 28, 2023, 11:22am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/3 "2023-09-28T11:22:40Z")

</div>

> [@Beatrix\_Willius](#):
>
> `loadForeignData `

Or

> [@Beatrix\_Willius](#):
>
> `dim theGetter as new GetImapStatus(BundleID)`

So maybe inside loadForeignData? Or something in theGetter ?

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [September 28, 2023, 11:24am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/4 "2023-09-28T11:24:00Z")

</div>

> [@Beatrix\_Willius](#):
>
> 2023-09-28 12:47:27 GetImapStatus.Constructor

Could it be inside of this constructor? Since you don’t see that one in the log with exeption?

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [September 28, 2023, 11:39am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/5 "2023-09-28T11:39:59Z")

</div>

LoadForeignData is a special test mode for debug mode only. The first line of GetImapStatus.Constructor is logged, too:

`Globals.theErrorLog.logitem currentMethodName, "open"`

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [September 28, 2023, 11:43am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/6 "2023-09-28T11:43:44Z")

</div>

Where is this in the log?

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [September 28, 2023, 12:07pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/7 "2023-09-28T12:07:31Z")

</div>

The line is in the first log where everything works. The line is missing when I get the NOE.

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [September 28, 2023, 1:23pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/8 "2023-09-28T13:23:05Z")

</div>

So there could be something Nil in there?

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [September 28, 2023, 1:33pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/9 "2023-09-28T13:33:25Z")

</div>

Can you use remote debugger to step through the code? It can be difficult to set this up, but ultimately worth the time spent.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [September 28, 2023, 1:45pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/10 "2023-09-28T13:45:37Z")

</div>

> [@Mike\_D](#):
>
> Can you use remote debugger to step through the code?

Good idea!

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [September 28, 2023, 2:56pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/11 "2023-09-28T14:56:11Z")

</div>

> [@Beatrix\_Willius](#):
>
> `GetImapStatus`

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.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [September 29, 2023, 12:11pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/12 "2023-09-29T12:11:30Z")

</div>

Argh…

Remote debugging turned out to be easy to set up. The NilObjectException comes from

`Redim ImapStatus(-1)`

Makes me feel really stupid.

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [September 29, 2023, 12:59pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/13 "2023-09-29T12:59:23Z")

</div>

How does Redim() cause a NOE? is there a destructor?

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [September 29, 2023, 1:39pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/14 "2023-09-29T13:39:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [September 29, 2023, 3:11pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/15 "2023-09-29T15:11:47Z")

</div>

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.

Norman filed a ticket to make this behavior more logical after our discussion: [#74264 - functions that return arrays need to return an empty array of the right type by default NOT NIL !](https://tracker.xojo.com/xojoinc/xojo/-/issues/74264)

There is a sample project on the ticket that you can play with to see the behavior for yourself.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [September 29, 2023, 4:10pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/16 "2023-09-29T16:10:16Z")

</div>

> [@Beatrix\_Willius](#):
>
> > 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.

Can you post sample code of ‘theGetter.getAccounts’?

If there are accounts you return an array with the accounts, right? if there are no accounts, you don’t return an empty array?

I just want to understand how ‘theGetter.getAccounts’ assigns a Nil here. Thanks.

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [September 29, 2023, 7:59pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/17 "2023-09-29T19:59:49Z")

</div>

> [@Tim\_Parnell](#):
>
> 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 🙂

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [September 30, 2023, 12:23am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/18 "2023-09-30T00:23:18Z")

</div>

If it’s really a Xojo WTF (dangerous foot-gun) maybe post it to the [https://forum.xojo.com/t/xojo-wtf](https://forum.xojo.com/t/xojo-wtf) thread?

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [September 30, 2023, 12:42am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/19 "2023-09-30T00:42:57Z")

</div>

> [@Tim\_Parnell](#):
>
> 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.

This seems like logical and expected behavior to me.

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [September 30, 2023, 1:00am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/20 "2023-09-30T01:00:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [September 30, 2023, 1:34am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/21 "2023-09-30T01:34:35Z")

</div>

If you have a function

`MyFunction() as MyClass`

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.

[Next page](https://forum.xojo.com/t/odd-nilobjectexception/77444.md?page=2)
