Hi Guys,

I am getting an error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "jLabel4"

With this code:

        public void insertCustomer() {
            Properties conProps = new Properties();
          conProps.setProperty("user", "user");
        conProps.setProperty("password", "pass");
         int q = Integer.parseInt(jLabel4.getText());

     try {
             con = DriverManager.getConnection("jdbc:mysql://91.208.99.2:3379/5thfloor_pos", conProps);
                con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
            } catch (SQLException ex) {
                Logger.getLogger(AddProduct.class.getName()).log(Level.SEVERE, null, ex);
            }

            String sql = ("INSERT INTO order_prty(customer, barcode, total) VALUES (?,?, ?)");

            try {

                con.setAutoCommit(false);

                st = (com.mysql.jdbc.PreparedStatement) con.prepareStatement(sql);
               st.setString(1, customer.getSelectedItem().toString());
                 st.setString(2, jTextField3.getText().toString());
                 st.setInt(3,q);
                st.execute();
                st.close();
                con.commit();
              //  JOptionPane.showMessageDialog(null, "saved!");

            } catch (Exception se) {
                se.printStackTrace();
            }
    }

Any ideas?

Greetings london-G!
Try putting a breakpoint at line 5 and then running the debugger to see what value you're getting for jLabel4. If it's not a string (i.e. - text) or if it's null, then that will help you find a solution. :)

Tekkno

TekknoDraykko: maybe you should re-check the error message.
The actual text he is trying to parse into a number is: "jLabel4", that is what the exception message says.

longon-G: most likely, you wanted to put a jTextField there, instead of a JLabel, since a JLabel is not really used for input.

     int q = Integer.parseInt(jLabel4.getText());

There is your problem. this line is basically:

     int q = Integer.parseInt("jLabel4");

and since "jLabel4" is not a numerical value which can be parsed to an int, you get that error. make sure you try to parse the text of the correct field and only when it has a valid value.

Thanks Guys,

As advised, I changed it to a textfield and set it a value. It worked!

Yep, my mistake. I mistook the string for a JLabel control (guess I should slow down when reading these ;) ).

Good job london-G.

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.