0

So this has been really bothering me: I am getting a number format exception somewhere in the following code (this is on a button btw, am doing a GUI for school):

    String provider = clientInfo.getMediAidProvider();
    String line = "";
    int totalCost = treatmentInfo.getTotalCost();
    System.out.println("P" + totalCost);
    double percentPayout = 0.0;
    double totalPayed = 0.0;
    double toBePayed = 0.0;
    int max = 0;
    int min = 0;

    try {
        Scanner sc = new Scanner(new File(provider + "Momentum Algorithm.txt"));
        while (sc.hasNextLine()) {
            line = sc.nextLine();


            if (line.contains("*")) {

                String pPayout = line.substring(line.indexOf("=") + 1, line.indexOf("%"));
                percentPayout = Integer.parseInt(pPayout);
                totalPayed = totalCost * (percentPayout / 100);
                toBePayed = totalCost - totalPayed;
                System.out.println("D" + percentPayout);
                System.out.println("E" + totalPayed);
                System.out.println("?" + toBePayed);

            } else {
                max = Integer.parseInt(line.substring(line.indexOf("~") + 2, line.indexOf("=") - 1));
                min = Integer.parseInt(line.substring(0, line.indexOf("~") - 1));
                System.out.println("{" + min);
                System.out.println("/" + max);
                if (totalCost >= min && totalCost <= max) {
                    String pPayout = line.substring(line.indexOf("=") + 1, line.indexOf("%"));
                    percentPayout = Integer.parseInt(pPayout);
                    totalPayed = totalCost * (percentPayout / 100);
                    toBePayed = totalCost - totalPayed;
                    System.out.println("+" + percentPayout);
                    System.out.println("0" + totalPayed);
                    System.out.println("*" + toBePayed);
                    break;
                }

            }

        }

    } catch (FileNotFoundException | NumberFormatException e) {
        System.out.println(e);
    }

    tblInvoice.setValueAt("R" + totalCost, 0, 0);
    tblInvoice.setValueAt(percentPayout + "%", 0, 1);
    tblInvoice.setValueAt("R" + totalPayed, 0, 2);
    tblInvoice.setValueAt("R" + toBePayed, 0, 3);

Essentially I am bringing in objects, converting to strings and then to integers or doubles, or just going straight from objects to ints. Here is the error:

    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)

Plus many more locations, but none of them actually link to my GUI Class or any other custom created class. So here is the problem: it gives me the exact problem, NumberFormatException, but nowhere does it tell me WHERE the error is. I have tried using System.out.println to trace through the code and it all seems to be running fine. I have no idea WHAT object is being passed to the Integer.parseInt method that it won't accept or where it is passing the object. Any help will be much appreciated!

pzaenger
  • 11,381
  • 3
  • 45
  • 46
  • What is `tblInvoice` ? – PeterMmm Aug 09 '16 at 17:36
  • Use `e.printStackTrace()` Instead of `System.out.println(e);` – OneCricketeer Aug 09 '16 at 18:02
  • The error message says the error occurs on line 507. Can you show us which line that is? – Phil Freihofner Aug 09 '16 at 18:02
  • You should try/catch the `NumberFormatException` solely when you're parsing that number, and then continue or break your loop from there. Then you also have the capability to print out the bad number in the form of debug. – Rogue Aug 09 '16 at 18:02
  • @PhilFreihofner Yeah, of `java.text.DecimalFormat`, not the code in the question. – OneCricketeer Aug 09 '16 at 18:03
  • Did you try to add in the catch block not only println exception, but its stack trace, like this `e.printStackTrace(); ` – lenach87 Aug 09 '16 at 18:05
  • The error happens while rendeing the table. You told the table to have numerical columns but you dont fill numbers. (Most likely the R prefix?). Maybe it is best to remove all swing TableFormatRenderers. – eckes Aug 09 '16 at 18:16
  • @Maverick So, show us what is `tblInvoice` (JTable ?) and how do you instatiate the parts of this object. – PeterMmm Aug 09 '16 at 18:18
  • tblInvoice is the table which I am trying to display my output data in. – Maverick Aug 10 '16 at 05:39
  • 1
    @eckes you hit the nail on the head - my table was set up to accept ints and doubles, not strings. Thanks m8 – Maverick Aug 10 '16 at 05:43
  • Thanks for upvote :) – eckes Aug 10 '16 at 18:04
  • Possible duplicate of [What is a NumberFormatException and how can I fix it?](http://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) – xenteros Oct 14 '16 at 10:13

1 Answers1

0

Looks like your tblInvoice is a kind of a table, which method setValueAt accept first parameter as a pointer to a row "R + number" For expample

tblInvoice.setValueAt("R" + totalPayed, 0, 2);

with a double type totalPayed

totalPayed = totalCost * (percentPayout / 100);

will be evaluated smth like this:

tblInvoice.setValueAt("R45.12", 0, 2);

which is probably not a good row number

Anyway exception appears in a GUI thread, not in your code, so the first thing to check - is your parameters that you send to GUI elements

Denis Yakovlev
  • 487
  • 3
  • 12