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.
|







