James Will
James Will

Reputation: 15

GUI calculate button coding error in java

Okay, first of all the project I am working on has two packages, first is murach.business which contains invoiceCalculations.java Second is murach.forms which contain InvoiceForm.java(JFrame) and SwingValidator.java

I need to add an event handler for the actionPerformed event of the Calculate button. Add code to this event handler that gets the customer type and subtotal from the form, uses the calculateDiscountPct method in the InvoiceCalculations class to calculate the the discount percent based on those values, calculates the discount amount and total, and formats and displays the values on the form.

The form has 5 rows for instance first customer type, subtotal, discount percent, discount amount and total and below that is calculate and exit button.

Here is the stack trace:

run:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "R"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
    at java.lang.Double.parseDouble(Double.java:510)
    at murach.forms.InvoiceForm.calculateButtonActionPerformed(InvoiceForm.java:183)
    at murach.forms.InvoiceForm.access$300(InvoiceForm.java:15)
    at murach.forms.InvoiceForm$4.actionPerformed(InvoiceForm.java:81)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6382)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6147)
    at java.awt.Container.processEvent(Container.java:2083)
    at java.awt.Component.dispatchEventImpl(Component.java:4744)
    at java.awt.Container.dispatchEventImpl(Container.java:2141)
    at java.awt.Component.dispatchEvent(Component.java:4572)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.java:2127)
    at java.awt.Window.dispatchEventImpl(Window.java:2489)
    at java.awt.Component.dispatchEvent(Component.java:4572)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:704)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:663)
    at java.awt.EventQueue$2.run(EventQueue.java:661)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:677)
    at java.awt.EventQueue$3.run(EventQueue.java:675)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:674)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 25 seconds)

So far I am getting error when i hit calculate button for my code. This is my current code, could you tell me what changes i need to make to code and why?

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
      // TODO add your handling code here:
   double c = Double.parseDouble(customertypeTextField.getText());
   double s = Double.parseDouble(subtotalTextField.getText());
   pct d = Percent.parsepct(discountpercentTextField.getText());
   double a = Double.parseDouble(discountamountTextField.getText());
   double total = InvoicCalculations.calculateTotal(c, s, d);
   NumberFormat currecny = NumberFormat.getCurrencyInstance();
   totalTextField.setText(currency.format(total));
}                                               

Upvotes: 1

Views: 1125

Answers (1)

One of the textfields contains a letter, causing Double.parseDouble to fail because it is not valid. From documentation:

Throws:

NullPointerException - if the string is null

NumberFormatException - if the string does not contain a parsable double.

To prevent this, you can:

  • ensure that when you hit the calculate button, the fields only contain valid data. You can do this by using an InputVerifier.
  • or verify before parsing that the input is valid, and display a popup or an error message if it isn’t.

Others have mentioned it, and it is important enough to be stressed here: do not use Double for representing money.

Upvotes: 1

Related Questions