0

I am trying to insert the one column "array" in SQL. I create my database on SQL and I used Java to create the table but when I tried to insert the values in the array in the SQL, I have this error?

private class Handler_c implements ActionListener{
    public void actionPerformed(ActionEvent arg0) {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3307/database_vf","root","");
            conn.setAutoCommit(false);
            Statement stmt1 = null;
            stmt1= conn.createStatement();
            String query1 ="CREATE TABLE result  (result INT)";
            stmt1.execute(query1);

            String query2 = "INSERT INTO result VALUES (?)";
            PreparedStatement pst1 = null   ;

            for(int i=0; i<array.length; i++)
            { pst1.setDouble(i+1, array[i]);}
            pst1 = (PreparedStatement) conn.prepareStatement(query2);
            pst1.execute();

            conn.commit();
            pst1.close();
            conn.close();
            System.out.println("Success result to mysql table");
         }

        catch(ClassNotFoundException e){
            System.out.println(e);
        }catch(SQLException ex){
            System.out.println(ex);
        }   
    }
  }

The error is :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at browse_gui$Handler_c.actionPerformed(browse_gui.java:191) ,,,,,,,,,,,,,,,,,,,,,,,,,,,,

This is line 191:

for(int i=0; i<array.length; i++) 
shree.pat18
  • 21,449
  • 3
  • 43
  • 63
lea
  • 23
  • 3
  • 1
    What error? You did not show us the error – Vince Dec 02 '15 at 23:50
  • i am sorry .... i am a beginner,,,,,,,,,,,,,,,,,,,,,,,the error is : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at browse_gui$Handler_c.actionPerformed(browse_gui.java:191) ,,,,,,,,,,,,,,,,,,,,,,,,,,,,this is line 191:for(int i=0; i – lea Dec 03 '15 at 00:02
  • You haven't initialized `array` anywhere in this code? – Emz Dec 03 '15 at 07:28
  • `array` is obviously null –  Dec 03 '15 at 07:28
  • Prove that `array` is concretely initialized. Actually, at least demonstrate that it's *defined*. – Makoto Dec 03 '15 at 07:30

0 Answers0