i am really new to java and netbeans... i want to send the data from jtable to database.after referring lot of codes i managed to come up with following code. but when i run it , it gives me the error: java.lang.NullPointerException. i really can't find where the error is.
this is my code,
try{
int rows = jTable2.getRowCount();
//int cols = jTable2.getColumnCount();
String myUrl = "jdbc:mySql://localhost:3306/hospital";
Connection con = DriverManager.getConnection(myUrl,"root","");
con.setAutoCommit(false);
String query = "insert into invoice (In_id,Admission_A_id,Hospital_Charges,Doctor_Charges,Doctor_visit_charge,Medicine_charge,Food_charge,Total) values (?,?,?,?,?,?,?,?)";
PreparedStatement pst = con.prepareStatement(query);
for(int row=0; row<rows; row++){
String st1 = jTable2.getValueAt(row, 0).toString();
int HosCharge = Integer.parseInt(st1);
String st2 = jTable2.getValueAt(row, 1).toString();
int DocCharge = Integer.parseInt(st2);
String st3 = jTable2.getValueAt(row, 2).toString();
int DocVisitCharge = Integer.parseInt(st3);
String st4 = jTable2.getValueAt(row, 3).toString();
int MedCharge = Integer.parseInt(st4);
String st5 = jTable2.getValueAt(row, 4).toString();
int FoodCharge = Integer.parseInt(st5);
String st6 = jTable2.getValueAt(row, 5).toString();
int Total = Integer.parseInt(st6);
pst.setInt(1,Integer.parseInt(tt1.getText()));
pst.setInt(2, Integer.parseInt(tt2.getText()));
pst.setInt(3,HosCharge);
pst.setInt(4,DocCharge);
pst.setInt(5, DocVisitCharge);
pst.setInt(6,MedCharge);
pst.setInt(7,FoodCharge);
pst.setInt(8,Total);
pst.addBatch();
}
pst.executeBatch();
con.commit();
con.close();
}
catch(Exception e){
e.printStackTrace();
}// TODO add your handling code here:
please help me to solve this.