0

I have this problem converting object into string... I make use of the toString() function... and since the conversion of object into string were inside the try{}catch(exception e){}, i keep on receiving an output error: For input string: ""

What should be the problem if i keep on receiving an error message like that?

More elaboration:

  1. the Object came from a jComboBox which consists of items from a database.
  2. I am using a JFrame Form instead of a Java Class.

All i want to do is to capture the selected Item from the JComboBox which happens to be an object. And then after capturing it. I'll use the value for my query in the database.

Here's my code(partial):

private void SUBMITActionPerformed(java.awt.event.ActionEvent evt) {                                       
    try {
        Class.forName(Connect.DRIVER);
        Connection con = DriverManager.getConnection(Connect.CONNECTION_STRING,
                Connect.USERNAME, Connect.PASSWORD);

        Object obj = jComboBox1.getSelectedItem();
        String item_name = obj.toString();

        int month = jMonthChooser.getMonth();
        int q_box = Integer.parseInt(quantity_box_txtbox.getText());
        double unit_price_box = 0;
        int q_pc = Integer.parseInt(quantity_pc_txtbox.getText());
        double unit_price_pc = 0;
        double sub_total_box = 0;
        double sub_total_pc = 0;
        double grand_total = 0;

        //Testing
        System.out.println(jMonthChooser.getMonth());
        System.out.println(item_name);

    } catch (Exception e) {
        System.out.println("Error: "+e.getMessage());
    }
}           

If you have anything you don't understand regarding with the way I explain my question please tell me... i'll try my best to elaborate further.

Thanks in advance.

:)

here's the complete error:

Error: java.lang.NumberFormatException : For input string: ""
AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
iamanapprentice
  • 411
  • 5
  • 19
  • 37

1 Answers1

3

Well, to start with:

  • Don't catch just Exception; catch specific subclasses
  • Don't just catch the exception; you almost certainly want to propagate it up to the caller
  • Don't log just the message - log the whole exception, including the stack trace and exception type.

The exception looks like it's trying to parse a string - not trying to convert an object to a string. I strongly suspect that the problem is one of these lines:

int q_box = Integer.parseInt(quantity_box_txtbox.getText());
int q_pc = Integer.parseInt(quantity_pc_txtbox.getText());

My guess is that one of the textboxes is empty - so you're effectively calling Integer.parseInt("") which is failing.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194