Article Preview
Buy Now
COLUMN
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
Article Description: No description available.
Article Length (in bytes): 6,208
Starting Page Number: 37
Article Number: 4513
Related Web Link(s):
http://www.declareSub.com/
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.