Error converting value to Jtable

Asked

Viewed 171 times

-1

I have an error when passing formatted value to the JTable, if anyone can help, I really appreciate.

This error only happens when I format the number, I wonder how I do to fix it.

Code:

    private void calcularButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    // TODO add your handling code here:
    // valor < 3.000 = 0.12
    // valor >= 3.000 && valor <= 5.000  = 0.18
    // valor >= 9.000 && valor <= 30.000  = 0.22
    // valor >= 40.000 && valor <= 90.000  = 0.24
    // valor >= 100.000 && valor <= 150.000  = 0.27
    // valor >= 160.000 && valor <= 210.000  = 0.31
    // valor >= 300.000 && valor <= 500.000  = 0.32
    // valor >= 1.000.000 && valor <= 2.000.000  = 0.34

    String posicao = posicaoTextField.getText();
    String nome = nomeTextField.getText();
    float valor = Float.valueOf(valorTextField.getText());
    float valorVenda = 0;

    DefaultTableModel dtm = (DefaultTableModel) valoresTableView.getModel();

    NumberFormat nf = NumberFormat.getCurrencyInstance();
    DecimalFormat df = new DecimalFormat("R$ ###,###.00");  



    if (valor < 3000) {
        valorVenda = (float) (valor * 0.095);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 3000 && valor <= 8000 ) {
        valorVenda = (float) (valor * 0.137);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 9000 && valor <= 39999) {
        valorVenda = (float) (valor * 0.163);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 40000 && valor <= 99999) {
        valorVenda = (float) (valor * 0.185);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 100000 && valor <= 159999) {
        valorVenda = (float) (valor * 0.191);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 160000 && valor <= 219999) {
        valorVenda = (float) (valor * 0.217);
        float valorTotalVenda = valor + valorVenda;
        System.out.println(nf.format(valorTotalVenda));
    }

    if (valor >= 300000 && valor <= 999999) {
        valorVenda = (float) (valor * 0.223);
        float valorTotalVenda = valor + valorVenda;
        float valorGanho = valorTotalVenda - valor;
        dtm.addRow(new Object[]{nome,
                                posicao,
                                df.format(valor).toString(),
                                df.format(valorTotalVenda),
                                df.format(valorGanho)});
    }

    if (valor >= 1000000 && valor <= 9999999) {
        valorVenda = (float) (valor * 0.239);
        float valorTotalVenda = valor + valorVenda;
        float valorGanho = valorTotalVenda - valor;
        dtm.addRow(new Object[]{nome,
                                posicao,
                                df.format(valor).toString(),
                                df.format(valorTotalVenda),
                                df.format(valorGanho)});
    }



}

And the error that comes up is this:

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.JTable$DoubleRenderer.setValue(JTable.java:5356)
at javax.swing.table.DefaultTableCellRenderer.getTableCellRendererComponent(DefaultTableCellRenderer.java:257)
at javax.swing.JTable.prepareRenderer(JTable.java:5723)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:290)
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)
  • 1

    You are passing some data that the numberformat is not able to format, but could not identify only with this excerpt, add a [mcve] of the code so that it is possible to test.

  • If the value is 8500, where it should fall?

  • In addition, the number logic described in the code comment does not match the logic implemented in the code below.

  • The comments were a small base, quiet, and the value of 8500 I corrected here: if (valor >= 4000 && valor <= 8999 ) {&#xA; float valorVenda = (float) (valor * 0.098);&#xA; float valorTotalVenda = valor + valorVenda;&#xA; float valorGanho = valorTotalVenda - valor;&#xA; dtm.addRow(new Object[]{nome,&#xA; posicao,&#xA; df.format(valor),&#xA; df.format(valorTotalVenda),&#xA; df.format(valorGanho)});&#xA; }

  • This code seems to be incomplete, the error breaks in the setvalueAt method, and this code has nothing to do with it.

  • I did a test, if I do not format, it passes normal to table, no errors, however, when format, it presents the error

Show 1 more comment

1 answer

1

Let’s first simplify your code:

private void calcularButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String posicao = posicaoTextField.getText();
    String nome = nomeTextField.getText();

    DefaultTableModel dtm = (DefaultTableModel) valoresTableView.getModel();

    NumberFormat nf = NumberFormat.getCurrencyInstance();
    DecimalFormat df = new DecimalFormat("R$ ###,###.00");  

    float valor = Float.parseFloat(valorTextField.getText());

    float fator = valor <    3000 ? 0.095f
                : valor <    9000 ? 0.137f
                : valor <   40000 ? 0.163f
                : valor <  100000 ? 0.185f
                : valor <  160000 ? 0.191f
                : valor <  300000 ? 0.217f
                : valor < 1000000 ? 0.223f
                : 0.239f;

    float valorVenda = valor * fator;
    float valorTotalVenda = valor + valorVenda;

    System.out.println(nf.format(valorTotalVenda));
    dtm.addRow(new Object[] {
            nome,
            posicao,
            df.format(valor).toString(),
            df.format(valorTotalVenda).toString(),
            df.format(valorVenda).toString()
    });
}

All that soup of ifs may be reduced to that tied ternary operator. Note that by this approach, gaps in its original code, such as the 8500 and the 250000, disappear. The resulting code is much smaller, simpler and less repetitive. Note the suffix f in the non-integer numbers.

I also put the code that adds a line on dtm out of all this, so that he may always be executed.

I also deleted the variable valorGanho that would always produce the same result as valorVenda. To prove it, see this:

    float valorTotalVenda = valor + valorVenda;
    float valorGanho = valorTotalVenda - valor;

Hence, by replacing the valorTotalVenda in the second equation, we have valorGanho = valor + valorVenda - valor, and therefore valorGanho = valorVenda.

  • Po, thanks, I was thinking about doing this later, but ok kkk, next, it worked, however, the formatting still shows error, I think it’s format error, I was looking here on google, but apparently I found nothing that could help me.

  • I got it bro, in Jtable’s settings, I changed the last three float columns to 'Object', there was kkkk. Sorry for the bother!! And thanks for all your attention

Browser other questions tagged

You are not signed in. Login or sign up in order to post.