Special

Clearance Sale!

We've been publishing for over five years now and it's time to clear out our inventory of back issues, so we're slashing prices!

RBD Magazines

Check out this amazing clearance sale of all our past issues. Missing some issues? This is a great time to complete your RBD collection. Save up to 40% off the regular price of our printed back issue packages. These prices are only good until the end of the year May 2008 and supplies are limited, so place your order today.

Article Preview


Buy Now

Print:
PDF:

Object-Oriented Thinking

Avoiding NilObjectExceptions

Issue: 4.5 (May/June 2006)
Author: Charles Yeomans
Article Description: No description available.
Article Length (in bytes): 5,026
Starting Page Number: 37
RBD Number: 4513
Resource File(s): None
Related Web Link(s):

http://www.declareSub.com/

Known Limitations: None

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.

Article copyrighted by REALbasic Developer magazine. All rights reserved.


 


|

 


Weblog Commenting and Trackback by HaloScan.com