JOptionPane.showInputDialog and the cancel button

so i'm using a JOptionPane.showInputDialog, and everything works except when i click the cancel button to exit the window, it gives me an error.
how can i remedy this?
here's a section of my code: (a is an array, as is b)
firstWord = JOptionPane.showInputDialog
("What company's sloagan is/was: "+a[count]+"?");
if(firstWord.equalsIgnoreCase(b[count]))
JOptionPane.showMessageDialog (null,
"Thats Right, the company was "+b[count]+".");
count++;
int YesNo = JOptionPane.showConfirmDialog (null,
"Do you want to play again?", "Choose One", JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(YesNo==1) //this should exit out if you click no
System.exit (0);
if(count==10)
JOptionPane.showMessageDialog(null,
"Game Over. You Won.");
System.exit (0);
else
JOptionPane.showMessageDialog (null,
"Sorry, you aren't that smart. Guess again.");
}]

1- Seems like you misstyped your code tags...
2- When you say: "it gives me an error", please provide details (e.g. stack trace)
3- Read the API doc, it says: showInputDialog returns user's input, or null meaning the user canceled the input.
So firstWord.equalsIgnoreCase(b[count]) will throw a NullPointerException in that case.
You might check for null value.
Note:str1.equals(str2) will throw a NullPointerException when str1 is null, but str2 can be null without a NullPointerException being thrown:String str1 = null;
String str2 = "toto";
System.out.println(str1.equals(str2)); // NullPointerException
System.out.println(str2.equals(str1)); // ok (prints false)

Similar Messages

  • How can I unsubscribe while the cancel button is u...

    There is no next payment for my subscription and the cancel button is missed. Is that mean the subscription will not automatically subscribe the next one until I confirm?

    I find that I will not have need of a skype account.  My free month is expired 09/22/14. I am a bit confused on how to do this on the web page - I do not see a"call phone tab" to go on to subscription settings.   So sorry.  I need your help with this.  Please cancel any renewal that may  be pending on my account.  Notify me with a confirmation on the cancellation.. Thank you very much.
    [Name redacted for privacy.]

  • How to use the cancel button in showInputDialog

    um how would i go with overriding the cancel button's deafult function in JOptionPane.showInputDialog?
    in my code:
    package alphaOne;
    * this program takes in a number and gets the factorial of that number
    import javax.swing.*;
    public class Factorial {
         public Factorial() {
               * variables intilization, input is where the option pane puts the data
               * number is teh actucal number entered parsed from string result is
               * where the resutl is stored flag1 is the indicator if the user had
               * entered an integer
              String input;
              int flag1 = 0;
              int number = 0;
              long result = 0;
               * this while loop runs continously unless an integer is entered
              while (flag1 != 1)
                   try {
                        input = JOptionPane
                                  .showInputDialog("Enter the number to be factored, Integer only: ");
                        number = Integer.parseInt(input);
                        flag1 = 1;
                   } catch (NumberFormatException ea) {
                        JOptionPane.showMessageDialog(null,
                                  "The input was not a integer!", "Warning",
                                  JOptionPane.ERROR_MESSAGE);
              result = number;
              for (int x = 1; x < number; x++) {
                   result = result * (x);
              JOptionPane.showMessageDialog(null, "The result is: " + result,
                        "Result", JOptionPane.CLOSED_OPTION);
              try {
                   System.exit(0);
              } catch (SecurityException ea) {
                   System.out.print("ARRRRR its dead!");
         public static void main(String[] args) {
              Factorial One = new Factorial();
    }the cancel button does not break the while loop

    what is the value of null compared to when you compare it, and do we use the String.equals() or the String == null to compare? as seen in here i tried with input.equals(null) as a comparison but it deos not exit when i press cancel
    package alphaOne;
    * this program takes in a number and gets the factorial of that number
    import javax.swing.*;
    public class Factorial {
         public Factorial() {
               * variables intilization, input is where the option pane puts the data
               * number is teh actucal number entered parsed from string result is
               * where the resutl is stored flag1 is the indicator if the user had
               * entered an integer
              String input;
              boolean flag1 = false;
              int number = 0;
              long result = 0;
               * this while loop runs continously unless an integer is entered
              while (flag1 != true){
                   try {
                        input = JOptionPane
                                  .showInputDialog("Enter the number to be factored, Integer only: ");
                        number = Integer.parseInt(input);
                        System.out.println(input);
                        flag1 = true;
                        if (input.equals(null))
                             try {
                                  System.exit(0);
                             } catch (SecurityException ea) {
                                  System.out.print("ARRRRR its dead!");
                   } catch (NumberFormatException ea) {
                        JOptionPane.showMessageDialog(null,
                                  "The input was not a integer!", "Warning",
                                  JOptionPane.ERROR_MESSAGE);
              result = number;
              for (int x = 1; x < number; x++) {
                   result = result * (x);
              JOptionPane.showMessageDialog(null, "The result is: " + result,
                        "Result", JOptionPane.INFORMATION_MESSAGE);
              try {
                   System.exit(0);
              } catch (SecurityException ea) {
                   System.out.print("ARRRRR its dead!");
         public static void main(String[] args) {
              Factorial One = new Factorial();
    }i have also tried to ue input == null, input == "", input.equals(""), input.equals(" ")
    Message was edited by:
    TheHolyLancer
    i edited the part so it is bold
    Message was edited by:
    TheHolyLancer

  • I entered time machine and wish to exit but the cancel button has no effect and neither does command quit.  I have no access to the Apple toolbar to use force quit.  How do I get out of this thing?????

    I opened the backup window of my time machine.  I now wish to exit.  The cancel button and Command quit have no effect.  Turning off the IMac and disconnecting the time machine have no effect. I have no access to the normal tool bar. I want outa here.  Help!

    As always.. pondini to the rescue..
    http://pondini.org/TM/D6.html
    (I got there via google search.. took less time than to type this reply).

  • I have my username, password, and sync key but I cannot click the next button to complete the sync setup?! Neither the next button nor the cancel button press?!

    I try to setup my sync account with Ubuntu 13.10 and I am able to enter my user name and password and recovery key but the next button will not click and neither will the cancel button. Only the back button, sync options button, and the x at the top of the screen are functioning?

    Did you enable the Side Bar?
    Select "Show Side Bar" from the "View" menu of iTunes.

  • When I enter time machine (on Time Capsule) i see the stack of screen shots and the time line. However, when I roll over the mouse pointer, the time line does not activate. The cancel button does not get me out of the app: have to alt cmd esc. Ideas?

    When I enter time machine (on Time Capsule) i see the stack of screen shots and the time line. However, when I roll over the mouse pointer, the time line does not activate. The cancel button does not get me out of the app: have to alt+cmd+esc. Ideas?

    I have never seen it but then I run SL which is much more reliable than Lion..
    See
    http://pondini.org/TM/E4.html
    Check the master guru of all TM problems.

  • I am walking through Apples tutorial getting started with iOS development. I am at the storyboard area and can't seem to drag the cancel button to the green exit. I am not sure why the exit button doesn't except it. Is anyone else having this issue?

    I am walking through Apples tutorial getting started with iOS development. I am at the storyboard area and can't seem to drag the cancel button to the green exit. I am not sure why the exit button doesn't except it. Is anyone else having this issue? Is there a work around? I have done this app twice and still cant get the exit to except the Cancel or Done  bar buttons

    Yes I checked it.  As far as I can see I did everything Apple said to do.  I took some screen shot so you can see how the screens are connected and what and where the code is, and what it does when I drag the cancel and done bar buttons to the exit

  • On my ipad if I accidentally press buy it now on ebay, the cancel button won't work and I end up buying something I don't want. What can I do

    If I accidentally press the Buy it now Button on ebay, I should be able to cancel it at checkout but the cancel button won't do this and I end up buying something I don't want.  Help ?

    Downloading (using iOS or computer) past purchases from the App Store, iBookstore, and iTunes Store - http://support.apple.com/kb/ht2519 - enabled with iTunes 10.3 and newer; not all media formats are available in all countries (see: iTunes in the Cloud and iTunes Match Availability by Country - http://support.apple.com/kb/HT5085); apps, books (not audiobooks), music, t.v. shows, and movies (some - not all studios have permitted this).  Downloading previously purchased movies and TV shows requires iTunes 10.6 or later.  Discontinued items not available. For items not included in the iCloud list (e.g., ringtones), or locations or computer systems where iCloud is not (yet?) available, you only get one download per fee paid.  Apple notes it is your responsibility to back up your purchases.
    Select the store on the left side of iTunes.
    Click on Purchased on the right side under Quick Links.
    You can re-download your available previous purchases.

  • How to disable the cancel button in the ProgressMonitor

    hi,
    I need to know, is there any way to disable/remove the (cancel)button in the ProgressMonitor?
    One more problem is,
    Once i click the cancel button, isCanceled() return true, how to make it false again so that the process continue....
    It is very urgently.....
    please help me out.
    Thanks in advance.
    Regards,
    Deepa Raghuraman

    I don't think that's a good solution, because Cancel button itself is not disabled, so user is tempted to click it and nothing happens.
    A better but dangerous solution is this:
    progressMonitor = new ProgressMonitor(ProgressMonitorDemo.this,
                                         "Running a Long Task",
                                         "", 0, 100);
    progressMonitor.setMillisToDecideToPopup(0);
    progressMonitor.setMillisToPopup(0);
    progressMonitor.setProgress(0);
    JDialog dialog = (JDialog)progressMonitor.getAccessibleContext().getAccessibleParent();
    JOptionPane pane = (JOptionPane)dialog.getContentPane().getComponent(0);
    pane.setOptions(new Object[]{});Refer to the same question here [http://stackoverflow.com/questions/512151/how-do-i-disable-the-cancel-button-when-using-javax-swing-progressmonitor] .

  • How to change the text in the Cancel button of JFileChooser ?

    Hello,
    In french the standart text in the Cancel button of the JFileChooser class is "Annuler". For a good reason which will be too long to explain, I need to change it by another term.
    I tried to create a super class and redefine the CANCEL_SELECTION string, but this does not work. I can't either change it directly because it has the final attribute.
    Where does JFileChooser get this word from ? ( I guess it is not hard coded for multilanguage support purpose).
    Any help would be appreciated
    Gege

    I believe the standard Look and feel implementations look in UIManager for a string called FileChooser.cancelButtonText.
    Therefore you might be able to change it after you've loaded your LnF by using the following code:
    UIManager.put("FileChooser.cancelButtonText", "---change me---");Note that this would change all file choosers in your application.
    If you only want to update one type of file chooser you could extend, for example, MetalFileChooserUI and set its cancel button text by overriding installStrings. You'd then need to install this UI on the file chooser you want to change.
    There may be an easier way to do this!
    Hope this helps.

  • Drag drop and ProgressBar cancel button

    Hi,
    I have a class that extends CDragDropTargetFlavorHelper, during ProcessDragDropCommand I want to show a ProgressBar (the code that does this is actually in another, model only, plugin - might be significant), however, though the progress bar shows the cancel button doesn't.  Unless I delay the processing until after the drop is complete by scheduling the command - if I either process the command or schedule and process scheduled commands the cancel button doesn't show.
    Scheduling the command gives a bit of a delay to the user (even with highest priority) and also no ability to feedback 'drop failed'.  So I'd like to:
    - preferably get the cancel button to show without scheduling the command
    - know immediately when a drop has been completed and process scheduled commands.
    Thanks in advance
    Ian

    Thanks Ned,
    I have since been continuing to research this and it would seem that implementing the HitTest could be the way to go. If anyone has any experience or advice to support this I would be grateful.
    Regards
    Chris

  • TS3297 hi. I am trying to change my billing address in itunes but it is not letting me do this. When I go to edit, I see only the 'cancel' button at the bottom of the page, no 'done' button to make changes. Why? I am a US registered itunes user but live o

    hi all. Hope someone can help me because I'm getting very frustrated and not finding answers! I am trying to change my billing address in my itunes store account, but itunes is not allowing me to do this. At the bottom of the page, I only get the 'cancel' button, no 'done' button to make changes. I am using an HP mini, and am a registered US itunes account holder, living outside the US right now (East Timor to be precise).
    Why won't the store let me change my info? I'm not having any other problems.

    The possible reasons are:
    1. Store credits in your account
    2. Pending downloads
    For more information contact iTunes Customer Support:
    http://www.apple.com/support/itunes/contact/

  • ESS Leave Request - Portal Runtime Error when the "Cancel" button is hit

    We have configured the Leave Request application, created a custom workflow, created security roles, and we are now testing in our quality environment. 
    We are encountering a very strange "Portal runtime error" which we cannot resolve - When you come into the Leave Request application, and then, without completing the request, just hit the "Cancel" button, the runtime error is generated.
    We have run a trace of authorizations and everything has come up clean.  Everything is configured, and there are no problems actually creating, approving and posting requests. 
    Any ideas?

    Hi,
    please check whether the user is authorized to cancel the request.
    and take a look at this WS12300111
    Regards
    Manohar

  • Capturing the Cancel button with Before Print event handler

    I put together a script that is triggered by a beforePrint event handler. Everything works fine but I can't figure out how and where to capture the user clicking on the Cancel button in the Print dialog?
    Any ideas? Thanks, Dan

    Alen,
    My bet is you are using JDeveloper 11.1.1.5. This is a known ADF bug 12551764 in this release, not related to JHeadstart.
    Here are the details:
    http://adfbugs.blogspot.com/2011/05/jdeveloper-11115-serius-bug.html
    I just checked the bug database, it is fixed in patch 12399372. You can go to metalink to download the patch.
    Steven Davelaar,
    JHeadstart Team.

  • HT5527 Each time I try to change my plan & purchase for ICloud storage the cancel button is my only option...the done option never lights up, how to I do the upgrade then?

    I am unable to purchase a downgrade or upgrade since the cancel button is my only option..cannot get to "done", what can I do to get upgrade completed?

    If you wish to continue your current storage level, the instructions are here:
    http://support.apple.com/kb/HT5527
    Scroll down to 'How do I purchase my current plan?' and expand the section applicable to your computer or device.

Maybe you are looking for

  • Lightroom 3 tethered 7d file naming

    I know there's other things similar to this that say 'known issue'... I figured I'd write this to help clear up what I was seeing.  Hopefully it's not all currently known, but if nothing else hopefully the next person googling finds this info useful.

  • Datasource return nothing

    Hi experts! If a datasource return no record how I can check this ?? using if condition And display an error message

  • Invisible raw images when importing

    I've done some reading of the forum and think I may have a unique problem. After I shoot raw images on my 20D I find that I can import the jpgs directly from the camera but the raw images do not. Any idea why? I can find them using the Canon Viewer u

  • My ipdo is charged and I can select a song but I press play the music doe snot play

    My ipod 5th generation is charged and loaded with music.  When I press play the musci does not play and the counter does not work.

  • Ipod froze during update

    Just tried to instal the latest update and my touch froze. Itunes displayed could not restore   ipod. Now my touch just diplays the connect to itunes picture. but itunes doesnt reconize it