This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes HOWTO check for click of Cancel button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "HOWTO check for click of Cancel button" Watch "HOWTO check for click of Cancel button" New topic
Author

HOWTO check for click of Cancel button

Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
Hey all,

I'm a beginner and am trying to learn a little GUI programming. I have a very simple program which asks for user input using an InputDialog. I can check for a non response but I can't figure out how to check for when the user clicks on the cancel button. Here's what I got so far:



When you click on the Cancel button, you get a NullPointerException. Where does this come from? An InputDialog accepts a String as input right? Is it that a String can not hold a null value?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8705
    
    6

I know I'm nitpicking, but the word HOWTO has a specific meaning. You aren't telling us how to do something, you are asking us how to do something.
About your question, have you checked the exception stack trace to see exactly what line in your code is throwing the NullPointerException? A String can hold a null value. Ask yourself if you are doing the right thing if a certain String is null.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Brian LaRue:
Is it that a String can not hold a null value?


I would hope so since your code assumes that this is so. Look at this line:

So, assuming that this is correct, that a string can be equal to null, what would happen if you took a null string and tried to determine its length?

On the bright side, your code is almost there.
Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
Here's my stacktrace:



I guess it's complaining about my line 33. It's the line that states:



Hmm..I tried something else and now it's fixed. I changed it to:



What's the difference between



and



? What really confuses me is that checking for a zero length on a String would generate the response that I was expecting. So, I never thought that it was that code that was wrong. I was thinking that checking for the null value was where my logic was in the can. Hmm...Well at least it's working now.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
IMO you have to have a string to check its length. If the string == null, then it doesn't exist, it has an undefined length and trying to get the length will throw an exception.
[ March 27, 2007: Message edited by: pete stein ]
Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
well I guess that makes more sense. However, how about this one: if a user types in a space then the length != 0 right? Should I go a step further and trim the whitespace?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you have your error-handling in the wrong order, and as 2 separate if's.

something like this should work Ok

Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
While searching the net and various APIs, I found a better way to do things. I discovered that you can customize buttons with your text and are able to control user input. That way it's less error-prone and there's only three different options. Here's my new code:



I'm guessing that there's a better way to phrase my conditional statements. Maybe I need a refresher in that category. I just have to get used to proper formatting and syntax I assume.

Is this a better way to do things? Am I making progress?
[ April 02, 2007: Message edited by: Brian LaRue ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HOWTO check for click of Cancel button
 
Similar Threads
Hard to explain. Deals with frames and Action Listener
If, Else IF
Help correcting a button behavior in swing
Swing/Applet question
help with joption pane cancel button