# 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:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![Anthony\_G\_Cyphers](https://forum.xojo.com/user_avatar/forum.xojo.com/anthony_g_cyphers/32/16862_2.png) [@Anthony\_G\_Cyphers](https://forum.xojo.com/u/Anthony_G_Cyphers)\
**Post date:** [September 30, 2023, 1:43am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/22 "2023-09-30T01:43:13Z")

</div>

I agree with @Tim_Parnell and Norman. An array is a collection, so I’d expect an empty box rather than a box that doesn’t exist. An array with no members makes more sense than Nil to me. There is, however, precedent for both elsewhere in the programming world with what seems to be a heavier leaning toward allow Nil (null, undefined) array references being possible.

Regardless, I suspect this change is just too far-reaching. I know that I never expect an array reference to be Nil even though I have run in to it in the past, but who knows how much code is out there that does rely on checking the validity of the reference? I don’t expect this to be changed.

---

<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:49am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/23 "2023-09-30T01:49:22Z")

</div>

The difference is `as MyClass`, that’s returning _a single instance._

If you were to define the return type `as MyClass()`, an array of MyClass objects, why would that be a _single_ nil instance?

Why would that even be able to be stored in an array variable? At _worst_ I would expect a `TypeMismatchException`, but would prefer the convenience of an empty array. I mean, convince is the whole point of using a framework.

Today I learned arrays much more duct-tape and bubble gum than I had previously expected.

---

<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, 2:07am UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/24 "2023-09-30T02:07:49Z")

</div>

> [@Tim\_Parnell](#):
>
> If you were to define the return type `as MyClass()`, an array of MyClass objects, why would that be a _single_ nil instance?

Because the function returns a _single_ array.

---

<div class="post-metadata">

**Author:** ![Ian\_Kennedy](https://forum.xojo.com/user_avatar/forum.xojo.com/ian_kennedy/32/6860_2.png) [@Ian\_Kennedy](https://forum.xojo.com/u/Ian_Kennedy)\
**Post date:** [September 30, 2023, 12:02pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/25 "2023-09-30T12:02:09Z")

</div>

But surely:

```auto
ReDim MyArray(-1)

```

Sets an array that exists to have no elements, not destroys the array completely. Otherwise you would’t be able to do:

```auto
ReDim MyArray(-1)
MyArray.Add "Some data"

```

As far as I am aware you can’t set an array to nil.

---

<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 30, 2023, 12:12pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/26 "2023-09-30T12:12:49Z")

</div>

> [@Ian\_Kennedy](#):
>
> As far as I am aware you can’t set an array to nil.

Sure, you can:

```auto
dim s(-1) as String
s = Nil

```

---

<div class="post-metadata">

**Author:** ![Ian\_Kennedy](https://forum.xojo.com/user_avatar/forum.xojo.com/ian_kennedy/32/6860_2.png) [@Ian\_Kennedy](https://forum.xojo.com/u/Ian_Kennedy)\
**Post date:** [September 30, 2023, 12:54pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/27 "2023-09-30T12:54:47Z")

</div>

Which is fine, I just wasn’t aware of it 🙂

ReDim shouldn’t be setting it to Nil though, otherwise:

```auto
ReDim MyArray(-1)
MyArray.Add "Some data"

```

Would throw a NilObjectException.

---

<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 30, 2023, 1:30pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/28 "2023-09-30T13:30:00Z")

</div>

> [@Ian\_Kennedy](#):
>
> ReDim shouldn’t be setting it to Nil though

Redim is not setting it to Nil, it was already Nil, then Redim throws the NilObjectException

```auto
Dim MyArray() As String
MyArray = Nil
Redim MyArray(-1)

```

---

<div class="post-metadata">

**Author:** ![Ian\_Kennedy](https://forum.xojo.com/user_avatar/forum.xojo.com/ian_kennedy/32/6860_2.png) [@Ian\_Kennedy](https://forum.xojo.com/u/Ian_Kennedy)\
**Post date:** [September 30, 2023, 1:32pm UTC](https://forum.xojo.com/t/odd-nilobjectexception/77444/29 "2023-09-30T13:32:24Z")

</div>

Ah… I see. Fair enough.

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