Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 17 in printed book and digital formats -- plus a one-year subscription (beginning with 18.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview

Buy Now

Issue 4.5


Avoiding NilObjectExceptions

Issue: 4.5 (May/June 2006)
Author: Charles Yeomans
Author Bio: Charles is the author of "I Declare: Calling External Functions in REALbasic", available online at http://www.declareSub.com/
Article Description: No description available.
Article Length (in bytes): 6,208
Starting Page Number: 37
Article Number: 4513
Related Web Link(s):


Excerpt of article text...

NilObjectExceptions are among the most common errors in REALbasic programming. In this issue's column, we take a crack at preventing them.

When you call a method or access a property of an object via a reference, REALbasic first checks to see if the reference is nil, which means that the reference does not refer to a valid object. Attempting to call a method or access a property using a nil reference is an error. Because REALbasic does not know how to handle this error in all situations, it raises a NilObjectException to give the caller a chance to handle the error. If the exception is not handled, the runtime displays a dialog and quits. This, by the way, is not a crash. A crash is what would happen if the runtime attempted to use the nil reference.

The rule for checking object references for nil is this: did you initialize the reference yourself using the New operator? If not, then you need to check the reference before using it, or be prepared to handle a NilObjectException. Note that functions like NewAppleEvent require checking. I note that I am still not being as careful as possible. Ideally, the New operator would either succeed or raise an exception, and this behavior would be documented.

...End of Excerpt. Please purchase the magazine to read the full article.