if ( obj.returnVoid() ) { throw("NullPointerException"); }
Posted on January 31, 2011 at 4:29 AM in ColdFusion
Okay, so this is relatively elementary, but since I did it _again_ I decided to go ahead and write a short post about it. Hopefully writing it out in paragraphs instead of simply fixing the screwup will help me actually remember it and think of this first the next time.
Take the following example method from our demo "object":
- public void function doFoo (
- required string foo
- )
- {
- // do foo; return nothing
- }
Pretty cool method, eh? Yeah, okay, so it'd be pathetic if it were in a real object, but since it's merely for the sake of demonstration, it works for me. :-) The point is that the doFoo() method returns, well, void.
Now then, let's say we have a service method that is interacting with this method. You are banging out some code and you think "It returns a boolean. Let's take a quick shortcut," and you write something like so...
- public any function interactWithFoo (
- )
- {
- var foo = getObjectFoo();
- if ( foo.doFoo() )
- {
- // do something
- }
- return foo;
- }
The highlight here is the if() statement, which is expecting a boolean value to be returned. However, in our code above we see that it returns void. The result? In CF9 you get basically no information:
java.lang.NullPointerException
That's it. No file name. No line numbers. Nada. Slightly, ummm, "lacking" in detail to assist you in troubleshooting the issue. So, when all else fails, just remember that trying to use a value from a method that returns void will result in the NullPointerException being thrown.
NOTE: Trying to use the value in any way will have the same result. It does not have to be contained in an if() statement; that is merely what I chose for this quickie example.
Latest Articles
- No recent entries.
Categories
- ColdBox (21) [RSS]
- ColdFusion (92) [RSS]
- Fusebox (3) [RSS]
- General (22) [RSS]
- jQuery (15) [RSS]
- Kalendar (1) [RSS]
- Linux (1) [RSS]
- Mura CMS (1) [RSS]
- Railo (1) [RSS]
- Rants (5) [RSS]
- Transfer (8) [RSS]
- Uni-Form Tag Library (36) [RSS]
Quick Links
Blogs I Read
Calendar
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| « Dec | ||||||
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Subscribe
Enter a valid email address.



There are no comments for this entry.
[Add Comment]