• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HOWTO check for click of Cancel button

 
Brian LaRue
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic