0

I am trying to deal with the exception Cancel Button causes to my program, but I cannot find the answer. (line 77 is the while () sentence)

String response = JOptionPane.showInputDialog(myJframe, "Please enter your Customer Card Code to view your Account Information");
while ( (response.equals(null)) || (response.equals("")) )    // if user typed nothing or pressed CANCEL button
{
    JOptionPane.showMessageDialog(myJframe, "You must enter a Customer Card Code!!!", "Error", JOptionPane.ERROR_MESSAGE);     // display error message
    response = JOptionPane.showInputDialog(myJframe, "Please enter your Customer Card Code to view your Account Information"); // ask again for the code until input is correct
}

My main goal is to manage to deal with the exception Cancel button causes when it returns a null value if pressed.

EDIT: RETURNED ERRORS

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MyWindow$2.actionPerformed(MyWindow.java:77)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ceid-vg
  • 323
  • 2
  • 9
  • 21
  • What exception? You've not yet posted the exception's stacktrace nor indicated which line throws it. Please fix this. – Hovercraft Full Of Eels Aug 07 '16 at 13:00
  • Oh, THAT exception. The heuristic for NullPointerExceptions is almost always the same. **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. In the future, please search on the subject before posting, since this is too common a problem to post yet another NPE question. – Hovercraft Full Of Eels Aug 07 '16 at 13:01
  • So you know response is null when cancel is pressed -- deal with it -- check for null **before** the while loop and then handle the situation appropriately. – Hovercraft Full Of Eels Aug 07 '16 at 13:02
  • Note this: `while (response.equals(null))` is not good and a null should never be checked this way. Use `==` always. – Hovercraft Full Of Eels Aug 07 '16 at 13:05
  • Please see: [Java null check why use == instead of .equals()](http://stackoverflow.com/questions/4501061/java-null-check-why-use-instead-of-equals) for more on your problem – Hovercraft Full Of Eels Aug 07 '16 at 13:06
  • SOLVED!! Hovercraft, the solution was the == instead of the .equals. If you want, type it as an answer so that I can give you rep – ceid-vg Aug 07 '16 at 13:11
  • ceid -- it's a duplicate -- again see the link directly above your last comment, or click here: [link](http://stackoverflow.com/questions/4501061/java-null-check-why-use-instead-of-equals) – Hovercraft Full Of Eels Aug 07 '16 at 13:20

1 Answers1

0
String response = JOptionPane.showInputDialog(myJframe, "Please enter your       Customer Card Code to view your Account Information");
while ( (response==null) || (response.equals("")) )    // if user typed nothing or pressed CANCEL button
{
    JOptionPane.showMessageDialog(myJframe, "You must enter a Customer Card Code!!!", "Error", JOptionPane.ERROR_MESSAGE);     // display error message
    response = JOptionPane.showInputDialog(myJframe, "Please enter your Customer Card Code to view your Account Information"); // ask again for the code until input is correct
}
Shekhar
  • 54
  • 5