1 2 Previous Next 23 Replies Latest reply: Nov 11, 2007 4:20 AM by DarrylBurke RSS

    java.lang.NullPointerException Error when updating Database

    807600
      Hi all,
      My java program is very basic and all i need to have done is have a new record inserted into my database when a specefic button is clicked and im using SQL starments to do this. But i'n getting this NullPointer exception when button is clicked and i don't know why.

      Here is my con() method to the database:
      public void con() 
           {
                try{
                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
                catch (ClassNotFoundException c)
                {System.out.println("unable to load database driver");}
                try
                { c=DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ= Guit.mdb");
                System.out.println("Connection Done");
                }
                catch(Exception e)
                {System.out.println("No Connection to dataBASE");}
                
           }
      okay and heres my updatetable method that my SQL statements uses
      public void updateTable(String update) throws SQLException{
                Statement stmt = c.createStatement();
                stmt.executeUpdate(update);
                stmt.close();
                
           }
      okay and lastly heres my addNew Method with my SQL Statement and my variables etc
      public void addNew() throws SQLException{
                a = JOptionPane.showInputDialog("Enter in a Brand");
                b = JOptionPane.showInputDialog("Enter in a Series");
                e = JOptionPane.showInputDialog("Enter in the Category");
                d = JOptionPane.showInputDialog("Enter in a Model");
                h = Integer.parseInt(JOptionPane.showInputDialog("Enter in the price"));
                j = Integer.parseInt(JOptionPane.showInputDialog("Enter in the stock"));
                f = JOptionPane.showInputDialog("Enter in the Pickup Layout");
                g = JOptionPane.showInputDialog("Enter in the Pckup Brand");
                updateTable("INSERT INTO Guitar VALUES ('"+a+"','"+b+"', '"+e+"', '"+d+"', '"+h+"', '"+j+"', '"+f+"', '"+g+"',)");
                System.out.print("Record Added");
      Okay a, b, e, d, f, and g are Strings while h and j are ints
      Guit is my database and guitar is my Table
      Any suggestions please i'm under a deadline and i'm stressing.....
        • 1. Re: java.lang.NullPointerException Error when updating Database
          807600
          OK now let's see the stack trace from your NullPointerException.
          • 2. Re: java.lang.NullPointerException Error when updating Database
            807600
            The Stack trace is of course the rest of the error?

            okay here it is
            Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
                 at editData.updateTable(editData.java:112)
                 at editData.addNew(editData.java:125)
                 at editData$8.actionPerformed(editData.java:335)
                 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
                 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
                 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
                 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
                 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
                 at java.awt.Component.processMouseEvent(Unknown Source)
                 at javax.swing.JComponent.processMouseEvent(Unknown Source)
                 at java.awt.Component.processEvent(Unknown Source)
                 at java.awt.Container.processEvent(Unknown Source)
                 at java.awt.Component.dispatchEventImpl(Unknown Source)
                 at java.awt.Container.dispatchEventImpl(Unknown Source)
                 at java.awt.Component.dispatchEvent(Unknown Source)
                 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
                 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
                 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
                 at java.awt.Container.dispatchEventImpl(Unknown Source)
                 at java.awt.Window.dispatchEventImpl(Unknown Source)
                 at java.awt.Component.dispatchEvent(Unknown Source)
                 at java.awt.EventQueue.dispatchEvent(Unknown Source)
                 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                 at java.awt.EventDispatchThread.run(Unknown Source)
            Line 112 is as follows
                      Statement stmt = c.createStatement();
            Line 125 is
                      updateTable("INSERT INTO Guitar VALUES ('"+a+"','"+b+"', '"+e+"', '"+d+"', '"+h+"', '"+j+"', '"+f+"', '"+g+"',)");
            And finally line 335 is my Actionperformed method, the line requesting
            addNew();
            • 3. Re: java.lang.NullPointerException Error when updating Database
              807600
              have you add the jconnector
              • 4. Re: java.lang.NullPointerException Error when updating Database
                807600
                Whats the jConnector?
                I have a connector method shown and its worked for displaying from the database but i can't add to the database now
                • 5. Re: java.lang.NullPointerException Error when updating Database
                  807600
                  c is null.

                  Perhaps you never set it. Perhaps you tried to but an exception was thrown. Perhaps you set a different variable called "c", which shadowed the field "c" that you meant to set. (This is one of many reasons why one-letter identifiers is a bad practice.) I notice that you re-used "c" to also be an exception; maybe you did that somewhere else as well. Or, perhaps you properly set c but then later reset it to null by accident.
                  • 6. Re: java.lang.NullPointerException Error when updating Database
                    807600
                    Okay i've gone through my entire class c is only mentioned 3 times, the first is when i declared it
                    public class editData extends JFrame {
                         Connection c;
                    The Second is in my con Method
                    c=DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ= Guit.mdb");
                    And of course lastly in my updateTable method
                    Statement stmt = c.createStatement();
                    so where do you think is it getting reset?
                    or where do you think is there an exception thrown?
                    • 7. Re: java.lang.NullPointerException Error when updating Database
                      807600
                      Well, if you look at your code, you'll see that you also have a variable "c" to catch ClassNotFoundExceptions. Of course, since you used single-letter variable names, it's really difficult to find things. If you had called your connection "connection", then your editor's search function would have helped you find it easily.

                      So who knows what you did? Maybe you have another "c" that you set to null.
                      • 8. Re: java.lang.NullPointerException Error when updating Database
                        807600
                        Also did you check your program output? Do you see a "Connecton Done" message?
                        • 9. Re: java.lang.NullPointerException Error when updating Database
                          807600
                          I've taken your advice and changed my connection name to connection,
                          my first few methods now lok like this
                          public class editData extends JFrame {
                               Connection connection;
                               String a;
                               String b;
                               String e;
                               String d;
                               int h;
                               int j;
                               String f;
                               String g;
                               String z;
                               public void con() 
                               {
                                    try{
                                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
                                    catch (ClassNotFoundException c)
                                    {System.out.println("unable to load database driver");}
                                    try
                                    { connection=DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ= Guit.mdb");
                                    System.out.println("Connection Done");
                                    }
                                    catch(Exception e)
                                    {System.out.println("No Connection to dataBASE");}
                                    
                               }
                               
                               public void updateTable(String update) throws SQLException{
                                    Statement stmt = connection.createStatement();
                                    stmt.executeUpdate(update);
                                    stmt.close();
                               }
                               public void addNew() {
                                    a = JOptionPane.showInputDialog("Enter in a Brand");
                                    b = JOptionPane.showInputDialog("Enter in a Series");
                                    e = JOptionPane.showInputDialog("Enter in the Category");
                                    d = JOptionPane.showInputDialog("Enter in a Model");
                                    h = Integer.parseInt(JOptionPane.showInputDialog("Enter in the price"));
                                    j = Integer.parseInt(JOptionPane.showInputDialog("Enter in the stock"));
                                    f = JOptionPane.showInputDialog("Enter in the Pickup Layout");
                                    g = JOptionPane.showInputDialog("Enter in the Pckup Brand");
                                    try {
                                         updateTable("INSERT INTO Guitar VALUES ('"+a+"','"+b+"', '"+e+"', '"+d+"', '"+h+"', '"+j+"', '"+f+"', '"+g+"',)");
                                    } catch (SQLException e1) {
                                         // TODO Auto-generated catch block
                                         e1.printStackTrace();
                                    }
                                    System.out.print("Record Added");
                               }
                          Okay anything else you can suggest? Becuase it still gives the same error as before....
                          • 10. Re: java.lang.NullPointerException Error when updating Database
                            807600
                            I didn't see the Connection Done msg i'll admit... what do you think is the cause?
                            • 11. Re: java.lang.NullPointerException Error when updating Database
                              807600
                              Where do you call con()? Maybe I'm just tired (it's nearly 2:00am here) but I don't see it. If you don't call con(), then of course connection (aka "c") will just have the default value, which is null.
                              • 12. Re: java.lang.NullPointerException Error when updating Database
                                807600
                                OH!!! of course its not called.... oops... wow 2am already you must live in California or somewhere near there....
                                • 13. Re: java.lang.NullPointerException Error when updating Database
                                  807600
                                  Yeah, CA.

                                  You might want to rename "con" into, say, "initializeConnection", and then you can do stuff in your code like:
                                  if (connection == null)
                                      initializeConnection();
                                  which is a lot easier to read.

                                  Or alternatively, you can make "con" smart enough to not only initialize it, but also check to see if it's non-null first, and if it is, to just return, or perhaps to check the status of the connection and perhaps re-initialize it if it's gone bad somehow.
                                  • 14. Re: java.lang.NullPointerException Error when updating Database
                                    807600
                                    OKAY>>> Now that my blonde moment is over we've gotten past the first error, it now says Connection done in the Console but NOW it says...
                                    Connection Done
                                    java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.
                                         at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
                                         at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
                                         at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
                                         at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
                                         at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source)
                                         at editData.updateTable(editData.java:72)
                                         at editData.addNew(editData.java:85)
                                         at editData$8.actionPerformed(editData.java:340)
                                         at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
                                         at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
                                         at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
                                         at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
                                         at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
                                         at java.awt.Component.processMouseEvent(Unknown Source)
                                         at javax.swing.JComponent.processMouseEvent(Unknown Source)
                                         at java.awt.Component.processEvent(Unknown Source)
                                         at java.awt.Container.processEvent(Unknown Source)
                                         at java.awt.Component.dispatchEventImpl(Unknown Source)
                                         at java.awt.Container.dispatchEventImpl(Unknown Source)Record Added
                                         at java.awt.Component.dispatchEvent(Unknown Source)
                                         at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
                                         at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
                                         at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
                                         at java.awt.Container.dispatchEventImpl(Unknown Source)
                                         at java.awt.Window.dispatchEventImpl(Unknown Source)
                                         at java.awt.Component.dispatchEvent(Unknown Source)
                                         at java.awt.EventQueue.dispatchEvent(Unknown Source)
                                         at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
                                         at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                                         at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                                         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                                         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                                         at java.awt.EventDispatchThread.run(Unknown Source)
                                    1 2 Previous Next