• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HOWTO check for click of Cancel button

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic