ceid-vg ceid-vg - 4 months ago 21
Java Question

Issues with Cancel Button in a Java message

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)

Answer
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
}