•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 360,993 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,561 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1110 | Replies: 3
So I've run into this situation a million times in my "learning career" and I've always found a long way around the issue so as not to risk throwing unnecessary exceptions..
Let's look at a simple example...
Throws java.lang.NullPointerException if null is passed to the method.
So, I've learned to check if an object is null before checking with its methods.
Both work just fine, because MyPanel.isEnabled() is only called if MyPanel is not null. It's a more efficient alternative to wrapping MyPanel.isEnabled() in a try-catch statement. No exception should ever be thrown in the above case. The second method is simple, as all is contained in a single line, but is there anything simpler?
Today I saw something like the following snippet on the Internet:
It combines the null-check and the data-check on the same line, separated by a logical operator. In this example, the logic is AND. If MyPanel is null, the first part of the check returns false, and MyPanel.isEnabled() is not called and no exception is thrown, even though a NullPointerException should be thrown. I suppose the false before the && is reason enough for the processor not to bother checking with whatever is after the &&, am I right?
Switching the order of the check to
Let's look at a simple example...
java Syntax (Toggle Plain Text)
boolean IsEnabled (JPanel MyPanel) { return MyPanel.isEnabled(); }
So, I've learned to check if an object is null before checking with its methods.
java Syntax (Toggle Plain Text)
boolean IsEnabled (JPanel MyPanel) { if (MyPanel == null) return false; else return MyPanel.isEnabled(); } // OR boolean IsEnabled (JPanel MyPanel) { return (MyPanel == null) ? false : MyPanel.isEnabled(); }
Today I saw something like the following snippet on the Internet:
java Syntax (Toggle Plain Text)
boolean IsEnabled (JPanel MyPanel) { return MyPanel != null && MyPanel.isEnabled(); }
Switching the order of the check to
MyPanel.isEnabled() && MyPanel != null results in the exception. I guess what I'm trying to confirm is, has the JVM always given precedence to the first argument in a logical operation, and will it always ignore successive arguments given the first is reason enough to "call it quits"? I try my best to keep things organized, and often I'd like to combine object checks into a single line at the beginning of a method call, just to be tidy. I think I've answered my own question by running the test above, but, should I feel safe in the future to combine null- and data-checks into a single logical statement without watching for exceptions?
Last edited by Cudmore : Dec 28th, 2006 at 2:02 pm.
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
How many people have code in their Sigs?
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
access breach broadband code com combo crime daniweb data data protection data transfer database development drive dropdownlist forensics government hard hardware hitachi internet java linux microsoft sdk for java microsystems module net news open platform programming reuse security software source storage sun terabyte web wikipedia
- Send data on a serial port (C++)
- data grabbing from html sites (Python)
- Data Validation checks (MS Access and FileMaker Pro)
- 2-D array (C++)
- C++ Data Types (C++)
Other Threads in the Java Forum
- Previous Thread: switch statement on String in Java
- Next Thread: connection pooling in JSP



Threaded Mode