# NilObject problem

**URL:** <https://forum.xojo.com/t/nilobject-problem/58115>\
**Category:** Windows\
**Created:** [October 20, 2020, 5:44pm UTC](https://forum.xojo.com/t/nilobject-problem/58115 "2020-10-20T17:44:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carl\_Mize](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Carl\_Mize](https://forum.xojo.com/u/Carl_Mize)\
**Post date:** [October 20, 2020, 5:44pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/1 "2020-10-20T17:44:33Z")

</div>

i have been having a problem with NilObject. it i want to walk through a run, i put a break at a point and it goes to there and when i take the first step, i get a nilobject. the line it rejected could be int1 = 1 and i have put a dim int1 as integer at the top of the module. also, if i move the break down stream, the program breaks at the new point and does not give me a nilobject before then and then gives me a nillobject when i take the first step onto a line that is ok and it had to execute the line that it broke at above. i have used xojo for years though not for a couple.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 20, 2020, 5:45pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/2 "2020-10-20T17:45:34Z")

</div>

Can you post the code? (Please use the code tags so it’s formatted for us.)

---

<div class="post-metadata">

**Author:** ![Carl\_Mize](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Carl\_Mize](https://forum.xojo.com/u/Carl_Mize)\
**Post date:** [October 20, 2020, 7:22pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/3 "2020-10-20T19:22:00Z")

</div>

i have a rather large program. i call a method to save a file like this

Dim Index, Id, int1 As Integer  
Dim str2 As String  
Dim f As FolderItem

bolSaveTraditionalResults = TRUE  
bolCalcResidVolume = False

f = GetSaveFolderItem(“TEXT”,"")

If f = Nil Then Exit Sub

TOS = TextOutputStream.Create(f)

if bolSaveTraditionalResults = true then  
TOS.WRITELINE strSaveLeadTraditional

i put the break at bolSaveTraditionalResults = TRUE and the variable is defined in the same module. when the program stops at the line, step in results in a nil object. if i move the break further down to the next line. i get to that line, then it gives a nil object

thanks.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [October 20, 2020, 8:09pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/4 "2020-10-20T20:09:05Z")

</div>

> [@Carl\_Mize](#):
>
> step in results in a nil object

Interesting. What happens if you Step Over instead? Step In would be unexpected at that point, so maybe there’s an oversight or something going on.

---

<div class="post-metadata">

**Author:** ![Carl\_Mize](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Carl\_Mize](https://forum.xojo.com/u/Carl_Mize)\
**Post date:** [October 20, 2020, 8:38pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/5 "2020-10-20T20:38:02Z")

</div>

i have used step in for years and never had this problem. i just tried step over and it gave me anil object also. i sure would like to figure out what is happening so i can get on with the program

---

<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:** [October 20, 2020, 8:46pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/6 "2020-10-20T20:46:46Z")

</div>

It sounds like you’re getting the `NilObjectException` on the `TOS.WRITELINE`. When the debugger pauses, you see a list of variables and their values. Check that `TOS` isn’t nil.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [October 20, 2020, 8:50pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/7 "2020-10-20T20:50:07Z")

</div>

I take it you’re declaring TOS and checking it’s not Nil?

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [October 20, 2020, 9:53pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/8 "2020-10-20T21:53:05Z")

</div>

Have you tried clearing the cache?

---

<div class="post-metadata">

**Author:** ![Carl\_Mize](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Carl\_Mize](https://forum.xojo.com/u/Carl_Mize)\
**Post date:** [October 20, 2020, 10:11pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/9 "2020-10-20T22:11:48Z")

</div>

i seldom work in this area of coding, copying it from program to program

i am presently at and TOS is declared a global text output stream

Dim f As FolderItem  
bolSaveTraditionalResults = TRUE  
bolCalcResidVolume = False  
f = GetSaveFolderItem(“TEXT”,"")  
If f = Nil Then Exit Sub  
TOS = TextOutputStream.Create(f)

i still get a nil object if i put a break on bolSaveTraditionalResults when i step in

how do i clear the cache?

---

<div class="post-metadata">

**Author:** ![Carl\_Mize](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Carl\_Mize](https://forum.xojo.com/u/Carl_Mize)\
**Post date:** [October 20, 2020, 10:13pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/10 "2020-10-20T22:13:52Z")

</div>

i have responded to another comment by tim hare with a longer response. to you i will say no and how do i check if it is nil? thx

---

<div class="post-metadata">

**Author:** ![Greg\_O\_Lone](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o_lone/32/49_2.png) [@Greg\_O\_Lone](https://forum.xojo.com/u/Greg_O_Lone)\
**Post date:** [October 20, 2020, 10:41pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/11 "2020-10-20T22:41:00Z")

</div>

Actually, if you’re using 2019r2 or later, you actually need to wrap your code in a try-catch block and “catch” the nilobjectexception. Just checking for nil won’t work any more.

---

<div class="post-metadata">

**Author:** ![Carl\_Mize](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Carl\_Mize](https://forum.xojo.com/u/Carl_Mize)\
**Post date:** [October 20, 2020, 10:49pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/12 "2020-10-20T22:49:09Z")

</div>

i am using the latest version. what is a try-catch block?

---

<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:** [October 20, 2020, 10:51pm UTC](https://forum.xojo.com/t/nilobject-problem/58115/13 "2020-10-20T22:51:02Z")

</div>

Some information:  
[https://documentation.xojo.com/api/code\_execution/try.html](https://documentation.xojo.com/api/code_execution/try.html)  
[https://documentation.xojo.com/getting\_started/debugging/exception\_handling.html](https://documentation.xojo.com/getting_started/debugging/exception_handling.html)

---

<div class="post-metadata">

**Author:** ![Carl\_Mize](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/dec6dc/32.png) [@Carl\_Mize](https://forum.xojo.com/u/Carl_Mize)\
**Post date:** [October 21, 2020, 12:54am UTC](https://forum.xojo.com/t/nilobject-problem/58115/14 "2020-10-21T00:54:37Z")

</div>

thx. the break on nil objection led to errors i could not imagine and were not in the stream of code being viewed. hopefully that will fix my problem.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [October 21, 2020, 5:52am UTC](https://forum.xojo.com/t/nilobject-problem/58115/15 "2020-10-21T05:52:59Z")

</div>

> [@Greg\_O\_Lone](#):
>
> Just checking for nil won’t work any more.

Is there some rationale for that?  
Wouldn’t it make changing code from API1 to API2 risky?

---

<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:** [October 21, 2020, 6:29am UTC](https://forum.xojo.com/t/nilobject-problem/58115/16 "2020-10-21T06:29:37Z")

</div>

Well that sounds like a major issue then. This should be headlined in the docs on each object…

---

<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:** [October 21, 2020, 6:45am UTC](https://forum.xojo.com/t/nilobject-problem/58115/17 "2020-10-21T06:45:02Z")

</div>

> [@Greg\_O\_Lone](#):
>
> Just checking for nil won’t work any more.

please could you explaining a little bit more? we all need/use this =nil universally.  
it was only about the use of GetSaveFolderItem?

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [October 21, 2020, 7:21am UTC](https://forum.xojo.com/t/nilobject-problem/58115/18 "2020-10-21T07:21:14Z")

</div>

> [@MarkusR](#):
>
> we all need/use this =nil universally

I have 700 instances in the project I have open alone, taking account of  
if x \<\> nil then  
and  
if x = nil then

Not all such cases are followed by code that could _or would need to_ be trapped in a try…catch

eg

```
if x = nil then
    msgbox "you need to do y"
end if

```

Try…catch doesn’t help there if we cannot rely on a test for nil

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [October 21, 2020, 9:28am UTC](https://forum.xojo.com/t/nilobject-problem/58115/19 "2020-10-21T09:28:33Z")

</div>

> [@Jeff\_Tullin](#):
>
> Not all such cases are followed by code that could _or would need to_ be trapped in a try…catch

Which is one of the irritating things about this exceptions-everywhere approach. I tend to put the IO operation (and nothing else) inside the Try.

Or you can do as I did with database methods. I already had my own wrapper around these, so I could encapsulate error logging without cluttering up the main code. So I hid the Try/Catch stuff in there too, and stuck with a single test after each database method call in the main code. Much cleaner.

---

<div class="post-metadata">

**Author:** ![Greg\_O\_Lone](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o_lone/32/49_2.png) [@Greg\_O\_Lone](https://forum.xojo.com/u/Greg_O_Lone)\
**Post date:** [October 21, 2020, 11:11am UTC](https://forum.xojo.com/t/nilobject-problem/58115/20 "2020-10-21T11:11:10Z")

</div>

We were talking about TextOutputStream, but you’re right GetFolderItem probably still returns Nil.

That said, the FolderItem API 2 methods all throw exceptions now so you’ll be better off learning how to do that.

[Next page](https://forum.xojo.com/t/nilobject-problem/58115.md?page=2)
