Member Avatar for David_83

Hi guys. So I'm building this code in java using NetBeans and literally this code has been working fine the entire time. I haven't changed anything about it and now it's giving me an error and I can't find out what's wrong. Can you please take a look for me? Thanks. Here's my code:

int MM = this.jComboBoxMM.getSelectedIndex();
        int DD = this.jComboBoxDD.getSelectedIndex();
        int YYYY = this.jComboBoxYYYY.getSelectedIndex();
        int HH = this.jComboBoxHH.getSelectedIndex();
        int mm = this.jComboBoxmm.getSelectedIndex();

        if(MM == 0 || DD == 0 || YYYY == 0 || HH == 0 || mm == 0) {
            jLabelInvalidDateTime.setVisible(true);
            try
            {
                Double.parseDouble(jTextFieldAmount.getText());
                jLabelInvalidAmount.setVisible(false);
            }
                catch(NumberFormatException e)
            {
                  //not a double
                jLabelInvalidAmount.setVisible(true);
            }
        } else {
            jLabelInvalidDateTime.setVisible(false);
            try
            {
                Double.parseDouble(jTextFieldAmount.getText());
                jLabelInvalidAmount.setVisible(false);
            }
                catch(NumberFormatException e)
            {
                  //not a double
                jLabelInvalidAmount.setVisible(true);
            }
        if(Double.parseDouble(jTextFieldAmount.getText()) < 0) {
            jLabelInvalidAmount.setVisible(true);
        } else {
            String DateTime = jComboBoxMM.getSelectedItem().toString() + "/" + jComboBoxDD.getSelectedItem().toString() + "/" + jComboBoxYYYY.getSelectedItem().toString() + " " + jComboBoxHH.getSelectedItem().toString() + ":" + jComboBoxmm.getSelectedItem().toString() + " " + jComboBoxAMPM.getSelectedItem().toString();
            DefaultTableModel model = (DefaultTableModel) jTableRunningTotal.getModel();
            int rows = jTableRunningTotal.getRowCount();
            model.setRowCount(rows - 3);
            if(jRadioButtonWithdraw.isSelected()){
                Double Amount = Double.parseDouble("-" + jTextFieldAmount.getText());
                model.addRow(new Object[]{jTextFieldLocation.getText(), jTextFieldContainer.getText(), Amount, DateTime, jComboBoxNameofPerson.getSelectedItem().toString(), jTextFieldReason.getText()});    
            } else {
                Double Amount = Double.parseDouble(jTextFieldAmount.getText());
                model.addRow(new Object[]{jTextFieldLocation.getText(), jTextFieldContainer.getText(), Amount, DateTime, jComboBoxNameofPerson.getSelectedItem().toString(), jTextFieldReason.getText()});
            }

            jTableRunningTotal.selectAll();
            double Total = 0.0;
            int selectedRow[] = jTableRunningTotal.getSelectedRows();
                for (int i : selectedRow) {
                    Total = Total + Double.parseDouble(jTableRunningTotal.getValueAt(i, 2).toString());
                }
            jTableRunningTotal.clearSelection();
            model.addRow(new Object[]{"","","","","",""});
            model.addRow(new Object[]{"","","","","",""});
            model.addRow(new Object[]{"Total","",Total,"","",""});

        //jMenuItemSave.setEnabled(true);
        jRadioButtonWithdraw.setSelected(false);
        jRadioButtonDeposit.setSelected(false);
        jLabelDateTime.setEnabled(false);
        jComboBoxMM.setEnabled(false);
        jComboBoxMM.setSelectedIndex(0);
        jComboBoxDD.setEnabled(false);
        jComboBoxDD.setSelectedIndex(0);
        jComboBoxYYYY.setEnabled(false);
        jComboBoxYYYY.setSelectedIndex(0);
        jComboBoxHH.setEnabled(false);
        jComboBoxHH.setSelectedIndex(0);
        jComboBoxmm.setEnabled(false);
        jComboBoxmm.setSelectedIndex(0);
        jComboBoxAMPM.setEnabled(false);
        jComboBoxAMPM.setSelectedIndex(0);
        jLabelInvalidDateTime.setVisible(false);
        jLabelInvalidDateTime.setEnabled(false);
        jLabelAmount.setEnabled(false);
        jTextFieldAmount.setEnabled(false);
        jTextFieldAmount.setText("0.00");
        jLabelInvalidAmount.setVisible(false);
        jLabelInvalidAmount.setEnabled(false);
        jLabelLocation.setEnabled(false);
        jTextFieldLocation.setEnabled(false);
        jTextFieldLocation.setText("");
        jLabelContainer.setEnabled(false);
        jTextFieldContainer.setEnabled(false);
        jTextFieldContainer.setText("");
        jTextFieldReason.setEnabled(false);
        jTextFieldReason.setText("");
        jButtonOK.setEnabled(false);
        jButtonCancel.setEnabled(false);
        jButtonClear.setEnabled(false);
        jLabelAddedBy.setEnabled(false);
        jComboBoxNameofPerson.setEnabled(false);
        jLabelReason.setEnabled(false);
        jTextFieldReason.setEnabled(false);
        jTextFieldReason.setText("");
        }
        }

Here's what I get from the Output Bar:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number
    at java.text.DecimalFormat.format(DecimalFormat.java:507)
    at java.text.Format.format(Format.java:157)
    at javax.swing.plaf.synth.SynthTableUI$SynthTableCellRenderer.configureValue(SynthTableUI.java:802)
    at javax.swing.plaf.synth.SynthTableUI$SynthTableCellRenderer.getTableCellRendererComponent(SynthTableUI.java:789)
    at javax.swing.JTable.prepareRenderer(JTable.java:5723)
    at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:683)
    at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:580)
    at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:364)
    at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:275)
    at javax.swing.JComponent.paintComponent(JComponent.java:780)
    at javax.swing.JComponent.paint(JComponent.java:1056)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JViewport.paint(JViewport.java:728)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
    at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

And here's a screenshot of the form to help you better visualize it:

Screen_Shot_2016-10-30_at_9_54_21_AM.png

Tell me if you need anything else. I'm fairly new to Java. Any help would be appreciated. Thank you.

Recommended Answers

All 10 Replies

Member Avatar for David_83

Hi rproffitt. Thank you for responding. My first guess is that it had something to do with the parsing of the doubles in lines 31, 39, and 42, but when I debugged the program, it said that the whole void was correct and it was return the correct values. The problem is that it stops halfway through the code and the output window returns that error, "Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Number" and stops the table from redrawing itself.

OK, the app runs up to line 42 and all looks OK. Now which line is troublesome?

Focus on that line and print or use a debugger to check the values to the calls.

It's interesting that the stack for your error doesn't include any of your code - it's just Swing re-drawing the JTable. So the exception is NOT being thrown from any code that you wrote. The implication is that you, at some earlier time, populated a cell that should be numeric with an object that isn't numeric or capable of being converted to a number, and this blows up when Swing tries to display the table.
So I would go back to the code where you populate the table's model (lines 40, 43 etc) and print all the values you use to populate numeric columns to see if any is unintended.

Member Avatar for David_83

JamesCherrill Yes exactly but I triple checked all of that and I never changed a thing and for some reason all of a sudden it's throwing back an error. I have six columns in jTableRunningTotal in order:
Location String
Container String
$ Double
Date & Time String
Added By Object
Reason String
The Output window is telling me that the object cant be converted to a number, whatever object it's trying to convert i have no freaking idea, but the funny thing is, I also have a save feature to the form:

try (Writer writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream("MoneyFormData.txt"), "utf-8"))) {
            jTableRunningTotal.selectAll();
            int selectedRow[] = jTableRunningTotal.getSelectedRows();
            for (int i : selectedRow) {
                writer.write(jTableRunningTotal.getValueAt(i,0).toString() + System.lineSeparator());
                writer.write(jTableRunningTotal.getValueAt(i,1).toString() + System.lineSeparator());
                writer.write(jTableRunningTotal.getValueAt(i,2).toString() + System.lineSeparator());
                writer.write(jTableRunningTotal.getValueAt(i,3).toString() + System.lineSeparator());
                writer.write(jTableRunningTotal.getValueAt(i,4).toString() + System.lineSeparator());
                writer.write(jTableRunningTotal.getValueAt(i,5).toString() + System.lineSeparator());
            }
        }       catch (IOException ex) {

        }

        jTableRunningTotal.clearSelection();
        jMenuItemSave.setEnabled(false);

and when I press it even after the jTable doesn't redraw itself, it still returns the values that I input when I press the Ok Button.
Before I press ok:
Screen_Shot_2016-10-30_at_5_23_24_PM.png

after i press ok:
Screen_Shot_2016-10-30_at_5_37_12_PM.png

the text document it saves values to:
Screen_Shot_2016-10-30_at_5_37_31_PM.png

Member Avatar for David_83

Would anybody like the project itself? I'm using NetBeans 8.1

Could it be the empty Strings ("") you added to the table at the end? Try zero instead for the numeric column?

Member Avatar for David_83

That's a good idea I'll try that. I changed the double to a string and then aligned the column with the cellrenderer but Id rather it be a double

Member Avatar for David_83

No that's not the problem because it adds up all the values before it re-adds the bottom three rows. It's not a big deal, I'll keep it set to string. It's just weird that it all of a sudden stopped working. Java is a pain in the ass haha or maybe it's just NetBeans. Thanks for the help. I'm marking this as resolved.

Maybe it's a thread-related problem - doing I/O on the swing thread, or updting the JTable from a thread other than the swing thread? Unless you are very careful you can get repaint events like the one that throws your exception when the program logic is not in a final or stable state.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.