Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7951613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:36:49+00:00 2026-06-04T02:36:49+00:00

I am trying to write a new record to two database tables which are

  • 0

I am trying to write a new record to two database tables which are called item and supplier.

I have a class that handles the database connection and SQL statements. I am using a large form in a class called ItemEntryScreen where I am using the following:

private void writeItemRecord()
{
     if ( DataBaseHandler.makeConnectionTofireplaceDB() == -1)
       {
           JOptionPane.showMessageDialog (frame, "Unable to connect to database table (Item)");
       }
     else  // Ok, so first read data from the text fields
       {
           // Read data from form and store data     
           String suppliercode = suppliercodeTxtField.getText();
           String suppliername = suppliernameTxtField.getText();
           String address = addressTxtField.getText();


           // Create a Item oject
           Item item = new Item();

           // Set the attributes for the Item object
           item.setSuppliercode(suppliercode);
           item.setSuppliername(suppliername);
           item.setAddress(address);

           // Write Item record.  Method writeToItemTable() returns
           // 0 of OK writing record, -1 if there is a problem.  I store
           // the returned value in a variable called error.
           int error = DataBaseHandler.writeTosupplierTable(item.getSuppliercode(),item.getSuppliername(),item.getAddress());

           // Check if there is a problem writing the record, in 
           // which case error will contain -1                                         
           if (error == -1)
             {
                 JOptionPane.showMessageDialog (frame, "Problem writing record to Item Table");
             }

          // Clear the form - actual method is coded below
          clearForm();

          // Close database connection.  Report an error message
          // if there is a problem.
          if ( DataBaseHandler.closeConnection() == -1 )
             {
                 JOptionPane.showMessageDialog (frame, "Problem closing data base conection");
             }
        }
    }                    

/**
 *  Method to write a Item record
*/
private void writesupplierRecord()
{
     // Check to see if we can connect to database table
     if ( DataBaseHandler.makeConnectionTofireplaceDB() == -1)
       {
           JOptionPane.showMessageDialog (frame, "Unable to connect to database table (Item)");
       }
     else  // Ok, so first read data from the text fields
       {
           // Read data from form and store data     

           String itemname = itemnameTxtField.getText();
           String itemcode = itemcodeTxtField.getText();
           String description = descriptionTxtField.getText();
           String unitprice = unitpriceTxtField.getText();
           String style = styleTxtField.getText();
           String finish = finishTxtField.getText();
           String stock = stockTxtField.getText();

           // Convert priceStr to a float
           Float fvar = Float.valueOf(unitprice);
           float newprice = fvar.floatValue();

           Float svar = Float.valueOf(stock);
           float newstock = svar.floatValue();

           // Create a Item oject
           Item item = new Item();

           // Set the attributes for the Item object
           item.setItemname (itemname);
           item.setItemcode (itemcode);
           item.setDescription (description);
           item.setUnitprice (newprice);
           item.setStock(newstock);
           item.setStyle(style);
           item.setFinish(finish);

           // Write Item record.  Method writeToItemTable() returns
           // 0 of OK writing record, -1 if there is a problem.  I store
           // the returned value in a variable called error.
           int error = DataBaseHandler.writeToitemTable(item.getItemname(),
                                                        item.getItemcode(),
                                                        item.getDescription(),
                                                        item.getUnitprice(), 
                                                        item.getStock(),
                                                        item.getStyle(),
                                                        item.getFinish()
                                                        );

           // Check if there is a problem writing the record, in 
           // which case error will contain -1                                         
           if (error == -1)
             {
                 JOptionPane.showMessageDialog (frame, "Problem writing record to Item Table");
             }

          // Clear the form - actual method is coded below
          clearForm();

          // Close database connection.  Report an error message
          // if there is a problem.
          if ( DataBaseHandler.closeConnection() == -1 )
             {
                 JOptionPane.showMessageDialog (frame, "Problem closing data base conection");
             }
        }

 }  // End

The above code compiles!

static public int writeToitemTable(String itemnameIn, String itemcodeIn, String descriptionIn,
                                        float unitpriceIn, float stockIn, String styleIn, String finishIn)
    {
          // Variable to hold the SQL query
          String SQLString;

          // Build a string containing the SQL INSERT instruction to be used later
          SQLString = "INSERT INTO item VALUES ('" + itemcodeIn + "','" + itemnameIn + "','" + descriptionIn + "','" + unitpriceIn + "','" 
                                            + stockIn + "','" + styleIn + "','" + finishIn + "')";


           try
              {
                    // The createStatement() method creates a Statement object.  Object will be
                    // attached to my reference variable (statement) defined at the top of class.
                    statement = connectionTofireplaceDB.createStatement();

                    // The executeUpdate() statement can be used here to execute an 
                    // SQL INSERT instruction.
                    statement.executeUpdate (SQLString);

              }
            catch (SQLException exception)
              {
                   return (-1);     // Return -1 if problem writing record to file

              }

            return (0);   // Return with 0 if record successfully written 

      } // End

  static public int writeTosupplierTable(String suppliernameIn, String suppliercodeIn, String addressIn)
    {
          // Variable to hold the SQL query
          String SQLString;

          // Build a string containing the SQL INSERT instruction to be used later
          SQLString = "INSERT INTO supplier VALUES ('" + suppliernameIn + "','" + suppliercodeIn + "','" + addressIn + "')";


           try
              {
                    // The createStatement() method creates a Statement object.  Object will be
                    // attached to my reference variable (statement) defined at the top of class.
                    statement = connectionTofireplaceDB.createStatement();

                    // The executeUpdate() statement can be used here to execute an 
                    // SQL INSERT instruction.
                    statement.executeUpdate (SQLString);

              }
            catch (SQLException exception)
              {
                   return (-1);     // Return -1 if problem writing record to file

              }

            return (0);   // Return with 0 if record successfully written 

      } // End          

When I enter details into the form and hit the submit button, I am getting a return value of -1 which results in a message box being displayed that states there was a problem writing to the database. Why?

Update

These are the error messages I can getting:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ItemEntryScreen.writeItemRecord(ItemEntryScreen.java:392)
    at ItemEntryScreen.actionPerformed(ItemEntryScreen.java:348)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6038)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
    at java.awt.Component.processEvent(Component.java:5803)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.awt.Component.dispatchEventImpl(Component.java:4410)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.java:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2429)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:36:50+00:00Added an answer on June 4, 2026 at 2:36 am

    First, print the stacktrace to find the error.

    Second, I have seen people are having these troubles because of “Statement”. Use “PreparedStatement” in places where the values are about to change.Most probably your issue will be solved if you do the second suggestion

    Third, close the connection using finally() block or something

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey I'm new to java servlets and I am trying to write one that
I'm pretty new to XQuery and I'm trying to write an example function that
I have a perplexing SQL select statement (ugly) that I'm trying to write in
I'm trying to write a linq to sql method that handles sorting, paging, and
I have a database and a Java Program. I am trying to write a
I am trying to get hibernate to write new objects each time I do
I am new to php and trying to write a login function. Bit stuck
I'm new to C# and trying to write a simple GUI Unit Test with
I'm new to cakephp and trying to write a simple app with it, however
I am new stored procedures and trying to write a procedure to duplicate user(by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.