Getting Java exception running the code on other machines

61 Views Asked by At

I made a project in Java, if I run the project from my machine everything works fine, but if I share the jar file and execute from another machine it does not work. I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "10,00"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:651)
    at model.components.Transaction.<init>(Transaction.java:33)
    at model.events.components.AddTransactionListener.actionPerformed(AddTransactionListener.java:87)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
    at java.desktop/java.awt.Component.processEvent(Component.java:6391)
    at java.desktop/java.awt.Container.processEvent(Container.java:2266)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I suspect this is the code that is producing the error:

 double transactionAmount = 0;
        try {
            if (addTransactionPanel.getAmountField().getText().equals("") || Double.parseDouble(addTransactionPanel.getAmountField().getText()) < 0.0)
                throw new MyException("Enter a valid amount!");
            transactionAmount = Double.parseDouble(addTransactionPanel.getAmountField().getText());
        } catch(Exception e) {
            errorMessage += e.getMessage() + "\n";
            errorCount++;
        }

addTransactionPanel.getAmountField().getText returns the text of a JTextField.

As I said before if I run the project on my machine I don't get the error (Im on Pop-os). I made a virtual machine with Windows 10 and it works fine. I tried the code on Ubuntu which is on another disk partition and it also works. Anyone can help me?

1

There are 1 best solutions below

0
MadProgrammer On

Without more context, I'm guessing that this is a localisation issue.

You could start by adding System.out.println(Locale.getDefault()); to your code to see what locale each platform is using.

Generally, you should be making use of JFormattedTextField or JSpinner to deal with the input format and then use their getValue method, which will actually perform the parsing, for example...

// By default, my locale is en_AU, so this is just for demonstration
// purposes, don't do this to your users
NumberFormat format = NumberFormat.getNumberInstance(Locale.GERMANY);
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
format.setRoundingMode(RoundingMode.HALF_UP);
JFormattedTextField textField = new JFormattedTextField(format);
textField.setColumns(10);
add(textField);
textField.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // Will need to cast the result, as it
        // returns Object by default
        System.out.println(textField.getValue());
    }
});

If I enter 10.10 into the field, it will output 1010, if I input 10,10 it will output 10.10

If I use Locale.getDefault() instead (ie en_AU in my case), if I enter 10.10, it will output 10.10, if I input 10,10, it will output 1010