File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Testing for Cancel Btn Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Testing for Cancel Btn" Watch "Testing for Cancel Btn" New topic
Author

Testing for Cancel Btn

Steve Booth
Greenhorn

Joined: Aug 18, 2004
Posts: 8
I'm using the numberString = JOptionPane.showInputDialog(stuff) command. When Clicking on the Cancel button, I'm getting differing responses in two programs. On one program (which works), I'm checking for a null response by:

if ( numberString.equals( null ) ) {
System.exit( 0 );
}

On another program, this generates the NULLEXCEPTION error.

The instructor suggested:

if ( numberString == null ) {
System.exit( 0 );
}


but couldn't explain why.

Help?

TIA
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Surely a difference in the programs.
Can you show us more code from the 'another' program?
From the declaration of numberString at least.


http://home.arcor.de/hirnstrom/bewerbung
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch, Steve!

I suspect that your different experiences could have been with different uses of a single program.

Note that the code

if ( numberString.equals( null ) ) {
System.exit( 0 );
}

will run just fine, so long as the reference numberString is not a null reference. The boolean test of the if-statement will evaluate to false and skip the body - System.exit(0);.

If numberString were a null reference, then the boolean test of the if-statement would through an exception because you can't ask a null reference to do anything, like invoke a method of an object that isn't there.

I'll try to illustrate this point with another example.

Consider that I have a class Dog, which defines that Dog-type objects have a behavior called eat.

I could possibly have code such as the following:

Dog spot = new Dog("Spot");
spot.eat();

and everybody would be happy (and well-fed). This code creates a dog object, gives it the name Spot, points the reference spot to this dog object, and then invokes the method eat of this dog object.

What would the following code mean?

Dog lost = null;
lost.eat();

What does this code do? It doesn't create a dog object. It initializes a Dog-type reference to null. It then tries to invoke the method eat on the dog object referenced by lost, but no dog object exists and lost doesn't refer to an object. That's a problem. In fact, that's a NullPointerException.

The lesson is: You can't invoke an instance method with a null reference.

---------------

if ( numberString == null )

This line of code is the proper way to test for a null reference. Note that no method is invoked with the reference.

Making sense?
[ September 17, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
 
subject: Testing for Cancel Btn
 
Similar Threads
HOWTO check for click of Cancel button
Closing an inner frame
exception handling
help with joption pane cancel button
JOptionPane and the default focus