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":

  1. public void function doFoo (
  2. required string foo
  3. )
  4. {
  5. // do foo; return nothing
  6. }

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

  1. public any function interactWithFoo (
  2. )
  3. {
  4. var foo = getObjectFoo();
  5. if ( foo.doFoo() )
  6. {
  7. // do something
  8. }
  9. return foo;
  10. }

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.

Comments
(Comment Moderation is enabled. Your comment will not appear until approved.)
CodeBassRadio

Latest Articles

Eventually something really brilliant and witty will appear right here.

Calendar

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

The Obligatory Wish List