0

Im trying to insert data from jtable to database!! the first three columns(stafftimetableid,staffname,staffid) are inserted from the jtexfield(no errors found,successfully added) but when im trying to insert from jtable it promts a java.null pointerExcetion error !!

I have no errors in database connection !!

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        if (jComboBox1.getSelectedItem().equals("Staff Time Table"))
        {
            try 
            {
                PreparedStatement pst =null;
                Connection con = clerkpanell.DBConnection.connectDB();
                String data=jTable2.getValueAt(0,1).toString();

                String sql = "insert into stafftimetable      (StaffTimeTableID,StaffName,StaffID,7.50-8.30) values ('"+ttid.getText()+"','"+staffname.getText()+"','"+staffid.getText()+"','"+data+"');";


                 pst=con.prepareStatement(sql);
                 pst.executeUpdate();
             //    JOptionPane.showMessageDialog(null,"Added");
            }
            catch (Exception e)

            {
                JOptionPane.showMessageDialog(null,e);

            }    
        }   

1 Answer 1

1

In this statement String sql = "insert into stafftimetable (StaffTimeTableID,StaffName,StaffID,7.50-8.30) values ('"+ttid.getText()+"','"+staffname.getText()+"','"+staffid.getText()+"','"+data+"');";

Please store the ttid.getText(), staffname.getText(),staffid.getText()into separate variables. Something like this,

String ttid=ttid.getText();
String staffname = staffname.getText();
String staffid = staffid.getText();

and then the insert statement should be something like this

String sql = "insert into stafftimetable      (StaffTimeTableID,StaffName,StaffID,7.50-8.30) values ('"+ttid.+"','"+staffname+"','"+staffid+"','"+data+"');";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.