I\'ve created the database and created my gui application. I\'m trying to insert
ID: 3816024 • Letter: I
Question
I've created the database and created my gui application. I'm trying to insert data into the table personnel by using the submit button action listener. I keep getting error. Can you show me what I need to do. Thanks. (I copied my code at bottom of assignment. )
The following is my assignment:
Gaddis 6e Chapter 17 Programming Challenge 6,7 p1170
1. Write an application that creates the database Personnel with 1 table Employee
2. Write a GUI application that allows the user to add new employees to the Personnel database.
Add your name to the Title
Insert these records into the table Employee:
package personnel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class NewEmployeeGUI extends JFrame
{
JPanel employeePanel; //create panel for the textfield and labels
JPanel buttonPanel; //create panel for submit and exit button
JLabel employeeIDLabel; //employee id label
JLabel nameLabel; //name label
JLabel positionLabel; // postion label
JLabel payRateLabel; //pay rate label
JTextField employeeIDTextField;
JTextField nameTextField;
JTextField positionTextField;
JTextField payRateTextField; //create text
JButton submitButton;
JButton exitButton;
public NewEmployeeGUI()
{
// Set the window title.
setTitle("CIS285 CH17 Assignment 9 Insert Employee");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Build the Query Panel.
buildEmployeePanel();
// Build the Button Panel.
buildButtonPanel();
// Add the panels to the content pane.
add(employeePanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
// Pack and display.
pack();
setVisible(true);
}
private void buildEmployeePanel()
{
// Create a panel.
employeePanel = new JPanel();
//create a
GridLayout gl = new GridLayout(4,1);
employeePanel.setLayout(gl);
JLabel employeeIDLabel = new JLabel("Employee ID");
JTextField employeeTextField = new JTextField(50);
JLabel nameLabel = new JLabel("Name");
JTextField nameTextField = new JTextField(50);
JLabel positionLabel = new JLabel("Postion");
JTextField positionTextField = new JTextField(50);
JLabel payRateLabel = new JLabel("Pay Rate");
JTextField payRateTextField = new JTextField(50);
employeePanel.setBorder(BorderFactory.createTitledBorder("Enter Employee Information"));
employeePanel.add(employeeIDLabel);
employeePanel.add(employeeTextField);
employeePanel.add(nameLabel);
employeePanel.add(nameTextField);
employeePanel.add(positionLabel);
employeePanel.add(positionTextField);
employeePanel.add(payRateLabel);
employeePanel.add(payRateTextField);
}
private void buildButtonPanel()
{
// Create a panel.
buttonPanel = new JPanel();
// Create the Submit button.
submitButton = new JButton("Submit");
// Register an action listener for the Submit button.
submitButton.addActionListener(new SubmitButtonListener());
// Create the Exit button.
exitButton = new JButton("Exit");
// Register an action listener for the Exit button.
exitButton.addActionListener(new ExitButtonListener());
// Add the two buttons to the panel.
buttonPanel.add(submitButton);
buttonPanel.add(exitButton);
}
/**
The SubmitButtonListener class is an action listener
for the Submit button.
*/
private class SubmitButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
final String DB_URL = "jdbc:sqlserver://localhost;instanceName=sqlexpress;" +
"databaseName=BuildPersonnelDB;integratedSecurity=true;create=true";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
try{
Connection conn = DriverManager.getConnection(DB_URL);
String EmployeeID = employeeIDTextField.getText();
String name = nameTextField.getText();
String position = positionTextField.getText();
String payRate = payRateTextField.getText();
// Create a Statement object.
Statement stmt = conn.createStatement();
// Create a string with an INSERT statement.
String sqlStatement = "INSERT INTO Personnel " +
"(EmployeeID, Name, Position, PayRate) " +
"VALUES ('" +
EmployeeID + "', " +
name + ", '" +
position + "', " +
payRate + "')";
// Send the statement to the DBMS.
int rows = stmt.executeUpdate(sqlStatement);
JOptionPane.showMessageDialog(null, " rows(s) added to the taable.");
conn.close();
}
catch (SQLException ex)
{
System.out.println("ERROR: " + ex.getMessage());
}
}
}
/**
The ExitButtonListener class is an action listener
for the Exit button.
*/
private class ExitButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
// End the application.
System.exit(0);
}
}
public static void main(String[] args) {
new NewEmployeeGUI();
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at personnel.NewEmployeeGUI$SubmitButtonListener.actionPerformed(NewEmployeeGUI.java:132)
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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(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)
Explanation / Answer
The exception thread "AWT-EventQueue-0" java.lang.NullPointerException .. comes when you try to reference or use a object which is not instantiated . As per your code 2 errors are possible: -
1. Your Connection conn is null referenced .. i.e. You are not able to connect to DB. Always try to place it in try catch block to handle the case when connection cannot be established. There is very less chance of this causing the exception though.
2. The most suspecious line which is causing this error is
String EmployeeID = employeeIDTextField.getText();
here you are trying to get text from employeeIDTextField object . But you have not initialized this object anywhere. So employeeIDTextField will always point to null. you need to instantiate employeeIDTextField before getting text from this. by using below statement where you are initializing nameTextField,positionTextField, payRateTextField.
JTextField employeeIDTextField = new JTextField (50);
Please try the second solution first. I think your program will start working.
Note that I cannot run your program becaus eI dont have access to your DB.
Hope this satisfies you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.