0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
maneesha
  • 145
  • 2
  • 4
  • 12
  • There is a line numer given with your NullPointerException. Search in that line which object can be null. – M. Haverbier May 12 '17 at 11:06
  • the error is giving for the' String st6 = jTable2.getValueAt(row, 5).toString();' this line. there are 6 columns in my table. is there something wrong in my indexing??? – maneesha May 12 '17 at 12:37
  • As the jTable2 seems to be there a line above (st5 = ...), it must have to do something with getValue(row, 5). Could be that there is no 6th element in that row? – M. Haverbier May 12 '17 at 12:40
  • when a start my indexing from 1, the error goes to the 'String st5 = jTable2.getValueAt(row, 4).toString();' this line which is changing as (row, 5) after i change the indexing – maneesha May 12 '17 at 12:44
  • yeah i thought so . that's why i tried changing the indexing. but still no use – maneesha May 12 '17 at 12:46
  • Btw, use small letters to start variable names (e.g. hosCharge instead of HosCharge) – M. Haverbier May 12 '17 at 13:14
  • oh ok.thanks..i'll look into that – maneesha May 12 '17 at 14:20

1 Answers1

1
String st6 = jTable2.getValueAt(row, 5).toString();

So the above statement is causing the problem.

If staring at the line of code doesn't help then do you know how to do basic problem solving???

The first thing to do is to simplify the code so that you only invoke one statment in each line of code. Additionally you can display the value of each variable used in the two statements:

System.out.println("Table: " + jTable2);
Object cellData = jTable2.getValueAt(row, 5);
System.out.println("Data: " + cellData);
String st5 = cellData.toString();

Now what do you think will happen? You will get a different line number telling you where the problem is so you will know the problem is either with the table variable or the data in the table. Once you know that you fix your problem.

This is standard debugging. You can't ask a question every time you get a simple NullPointerException!!!

camickr
  • 321,443
  • 19
  • 166
  • 288