Keyword

# Nil

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Used to determine if an object is <span class="title-ref">Nil</span> (no value).

## Usage

``` xojo
expression = Nil
```

## Notes

<span class="title-ref">Nil</span> means no value. When an object is first instantiated, it is automatically initialized to <span class="title-ref">Nil</span>. Some functions will return a <span class="title-ref">Nil</span> value under certain circumstances. `FolderItem<folderitem.showopenfiledialog>` will return <span class="title-ref">Nil</span> if the user clicks the Cancel button when `FolderItem<folderitem.showopenfiledialog>` presents the standard open file dialog box.

<span class="title-ref">Nil</span> is a constant of its own datatype, not an `Object</api/data_types/additional_types/object>`. It can convert itself to any object class, any array type, or any of the pointer types (`Ptr</api/data_types/additional_types/ptr>`, `Window<window.handle>`, `CString</api/data_types/additional_types/cstring>`, `PString</api/data_types/additional_types/pstring>`, `WString</api/data_types/additional_types/wstring>`, and `CFStringRef</api/data_types/additional_types/cfstringref>`).

You can pass <span class="title-ref">Nil</span> for any of the string parameter types above (`CString</api/data_types/additional_types/cstring>`, `PString</api/data_types/additional_types/pstring>`, `WString</api/data_types/additional_types/wstring>`), but not for `String</api/data_types/string>`. Passing <span class="title-ref">Nil</span> is not the same as passing the empty string, "". Passing <span class="title-ref">Nil</span> means you want to pass a null pointer. Passing "" means you want to pass a non-null pointer to the representation of "" for the declared format.

If you try to access an object that has a <span class="title-ref">Nil</span> value, a `NilObjectException</api/exceptions/nilobjectexception>` error will be generated. If you don't handle the exception, execution will stop. If you are testing the application in the IDE, you can choose Project \> Break on Exceptions to halt execution whenever a runtime exception error occurs and open the Debugger.

If the code executes in a built application, the user will see a generic error message before the application quits.

To prevent this, you should always test for <span class="title-ref">Nil</span> values, either with an `If</api/language/if>` statement or by including a `Try</api/language/try>` or `Exception</api/exceptions/exception>` block at the end of the method.

## Sample code

This example uses <span class="title-ref">Nil</span> to determine if the user clicked the Cancel button in the standard open file dialog box presented by the `FolderItem<folderitem.showopenfiledialog>` function.

``` xojo
Var f As FolderItem.ShowOpenFileDialog("")

If f <> Nil Then
  ' use the FolderItem
End If
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This example uses an `Try</api/language/try>` block to handle `NilObjectExceptions</api/exceptions/nilobjectexception>`.

``` xojo
Var d As Dictionary

Try
  d.Value("Name") = "Mary"
Catch err As NilObjectException
  ErrorLabel.Text = err.Message
End Try
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`RuntimeException</api/exceptions/runtimeexception>` class; `Exception</api/exceptions/exception>`, `Try</api/language/try>` statements; `NilObjectException</api/exceptions/nilobjectexception>` error.

</div>
