Hi I am new to this forum and want some help with some Java code. I am creating a Three Tier Structure and below is the GUI class of the project. The problem is I can't make it work to insert into the Database file. Could someone please help me with it? Thanks.

// GUI Visitor Register

package Register;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class VisitorRegister extends JFrame {

	// GUI variables
	// JLabel and JComboBox for Title
	private JLabel TitleJLabel;
	private JComboBox TitleJComboBox;

	// JLabel and JTextField for FirstName
	private JLabel FirstNameJLabel;
	private JTextField FirstNameJTextField;

	// JLabel and JTextField for LastName
	private JLabel LastNameJLabel;
	private JTextField LastNameJTextField;

	// JLabel and JTextField for CompanyName
	private JLabel CompanyNameJLabel;
	private JTextField CompanyNameJTextField;

	// JLabel and JComboBox for VisitorType
	private JLabel VisitorTypeJLabel;
	private JComboBox VisitorTypeJComboBox;

	// JLabel and JTextField for HostName
	private JLabel HostNameJLabel;
	private JTextField HostNameJTextField;

	// JLabel and JTextField for HostLocation
	private JLabel HostLocationJLabel;
	private JComboBox HostLocationJComboBox;

	// JButton for NewVisitor
	private JButton NewVisitorJButton;

	// JButton for Appointment
	private JButton AppointmentJButton;

	// JButton for CancelAppointment
	private JButton CancelAppointmentJButton;

	// JButton for StaffHost
	private JButton StaffHostJButton;

	// JButton for Login
	private JButton LoginJButton;

	// JButton for LogOff
	private JButton LogOffJButton;

	// no-argument constructor
	public VisitorRegister() {
		createUserInterface();
	}

	// create and position GUI components; register event handlers
	public void createUserInterface() {
		// get content pane and set layout to null
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		// set up TitleJLabel
		TitleJLabel = new JLabel();
		TitleJLabel.setText("Title: ");
		TitleJLabel.setBounds(15, 15, 30, 25);// done (x, y, w, h)
		contentPane.add(TitleJLabel);

		// set up TitleJComboBox
		String title[] = {"Dr", "Mr", "Mrs", "Ms"};
		TitleJComboBox = new JComboBox(title);
		TitleJComboBox.setBounds(50, 15, 50, 25);// done (x, y, w, h)
		contentPane.add(TitleJComboBox);

		// set up FirstNameJLabel
		FirstNameJLabel = new JLabel();
		FirstNameJLabel.setText("First Name: ");
		FirstNameJLabel.setBounds(105, 15, 70, 25);// done (x, y, w, h)
		contentPane.add(FirstNameJLabel);

		// set up FirstNameJTextField
		FirstNameJTextField = new JTextField();
		FirstNameJTextField.setText(" ");
		FirstNameJTextField.setBounds(180, 15, 125, 25);// done (x, y, w, h)
		contentPane.add(FirstNameJTextField);

		// set up LastNameJLabel
		LastNameJLabel = new JLabel();
		LastNameJLabel.setText("Last Name: ");
		LastNameJLabel.setBounds(320, 15, 70, 25);// done (x, y, w, h)
		contentPane.add(LastNameJLabel);

		// set up LastNameJTextField
		LastNameJTextField = new JTextField();
		LastNameJTextField.setText(" ");
		LastNameJTextField.setBounds(395, 15, 120, 25);// done (x, y, w, h)
		contentPane.add(LastNameJTextField);

		// set up CompanyNameJLabel
		CompanyNameJLabel = new JLabel();
		CompanyNameJLabel.setText("Company Name: ");
		CompanyNameJLabel.setBounds(15, 55, 100, 25);// done (x, y, w, h)
		contentPane.add(CompanyNameJLabel);

		// set up CompanyNameJTextField
		CompanyNameJTextField = new JTextField();
		CompanyNameJTextField.setText(" ");
		CompanyNameJTextField.setBounds(120, 55, 170, 25);// done (x, y, w, h)
		contentPane.add(CompanyNameJTextField);

		// set up VisitorTypeJLabel
		VisitorTypeJLabel = new JLabel();
		VisitorTypeJLabel.setText("Visitor Type: ");
		VisitorTypeJLabel.setBounds(315, 55, 85, 25);// done (x, y, w, h)
		contentPane.add(VisitorTypeJLabel);

		// set up VisitorTypeJComboBox
		String visitor[] = {"Cleaner", "Contractor", "Courier", "Staff", "Visitor"};
		VisitorTypeJComboBox = new JComboBox(visitor);
		VisitorTypeJComboBox.setBounds(395, 55, 120, 25);// done (x, y, w, h)
		contentPane.add(VisitorTypeJComboBox);

		// set up HostNameJLabel
		HostNameJLabel = new JLabel();
		HostNameJLabel.setText("Host's Full Name: ");
		HostNameJLabel.setBounds(15, 95, 100, 25);// done (x, y, w, h)
		contentPane.add(HostNameJLabel);

		// set up HostNameJTextField
		HostNameJTextField = new JTextField();
		HostNameJTextField.setText(" ");
		HostNameJTextField.setBounds(120, 95, 170, 25);// done (x, y, w, h)
		contentPane.add(HostNameJTextField);

		// set up HostLocationJLabel
		HostLocationJLabel = new JLabel();
		HostLocationJLabel.setText("Host Location: ");
		HostLocationJLabel.setBounds(305, 95, 85, 25);// done (x, y, w, h)
		contentPane.add(HostLocationJLabel);

		// set up HostLocationJComboBox
		String location[] = {"Ground Floor", "Lower Ground Floor", "1st Floor", "2nd Floor", "3rd Floor", "4th Floor", "5th Floor"};
		HostLocationJComboBox = new JComboBox(location);
		HostLocationJComboBox.setBounds(395, 95, 120, 25);// done (x, y, w, h)
		contentPane.add(HostLocationJComboBox);

		//set up NewVisitorJButton
		NewVisitorJButton = new JButton();
		NewVisitorJButton.setText("New Visitor");
		NewVisitorJButton.setBounds(35, 135, 150, 30); // done (x, y, w, h)
		contentPane.add(NewVisitorJButton);
        // anonymous inner class for ActionListener
        NewVisitorJButton.addActionListener(
                new ActionListener() {
                    // method called when calculate JButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        NewVisitorJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener

		// set up AppointmentJButton
		AppointmentJButton = new JButton();
		AppointmentJButton.setText("Make Appointment");
		AppointmentJButton.setBounds(190, 135, 150, 30);// done (x, y, w, h)
		contentPane.add(AppointmentJButton);
		// anonymous inner class for ActionListener
        AppointmentJButton.addActionListener(
                new ActionListener() {
                    // method called when calculate JButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        AppointmentJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener

		// set up CancelAppointmentJButton
		CancelAppointmentJButton = new JButton();
		CancelAppointmentJButton.setText("Cancel Appointment");
		CancelAppointmentJButton.setBounds(345, 135, 150, 30);// done (x, y, w, h)
		contentPane.add(CancelAppointmentJButton);
		// anonymous inner class for ActionListener
        CancelAppointmentJButton.addActionListener(
                new ActionListener() {
                    // method called when calculate JButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        CancelAppointmentJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener

		// set up LoginJButton
		LoginJButton = new JButton();
		LoginJButton.setText("Login Visitor");
		LoginJButton.setBounds(35, 175, 150, 30);// done (x, y, w, h)
		contentPane.add(LoginJButton);
		// anonymous inner class for ActionListener
        LoginJButton.addActionListener(
                new ActionListener() {
                    // method called when calculate JButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        LoginJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener

		// set up LogOffJButton
		LogOffJButton = new JButton();
		LogOffJButton.setText("Log Off Visitor");
		LogOffJButton.setBounds(190, 175, 150, 30);// done (x, y, w, h)
		contentPane.add(LogOffJButton);
		// anonymous inner class for ActionListener
        LogOffJButton.addActionListener(
                new ActionListener() {
                    // method called when calculate JButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        LogOffJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener

		// set up StaffHostJButton
		StaffHostJButton = new JButton();
		StaffHostJButton.setText("Add Staff / Host");
		StaffHostJButton.setBounds(345, 175, 150, 30);// done (x, y, w, h)
		contentPane.add(StaffHostJButton);
		// anonymous inner class for ActionListener
        StaffHostJButton.addActionListener(
                new ActionListener() {
                    // method called when StaffHostJButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        StaffHostJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener


		// set properties of application’s window
		setTitle("Visitor's Register"); // set title bar text
		setSize(540, 250); // set window size
		setVisible(true); // display window
}// end method createUserInterface

	// Methods for ActionListeners
	private void NewVisitorJButtonActionPerformed(ActionEvent event) {
		FirstNameJTextField.setText(" ");
		LastNameJTextField.setText(" ");
		CompanyNameJTextField.setText(" ");
		HostNameJTextField.setText(" ");
	}

	private void AppointmentJButtonActionPerformed(ActionEvent event) {
		String title = (String) TitleJComboBox.getSelectedItem();
		String firstName = FirstNameJTextField.getText();
		String lastName = LastNameJTextField.getText();
		String companyName = CompanyNameJTextField.getText();
		String visitor = (String) VisitorTypeJComboBox.getSelectedItem();
		String host = HostNameJTextField.getText();
		String location = (String) HostLocationJComboBox.getSelectedItem();

		VisitorDA.initialize();
		Visitor newVisitor = VisitorDA.newVisitor(rs.getString("title"), rs.getString ("fname"), rs.getString ("lname"), rs.getString ("compname"), rs.getString ("vistype"), rs.getString ("hname"), rs.getString ("location"));
//		Visitor newVisitor = VisitorDA.newVisitor(title, firstName, lastName, companyName, visitor, host, location);

		VisitorDA.terminate();
	}// end method AppointmentJButtonActionPerformed

	private void CancelAppointmentJButtonActionPerformed(ActionEvent event) {
	}

	private void LoginJButtonActionPerformed(ActionEvent event) {
	}

	private void LogOffJButtonActionPerformed(ActionEvent event) {
	}

	private void StaffHostJButtonActionPerformed(ActionEvent event) {
	}

	// main method
	public static void main(String[] args) {
		VisitorRegister application = new VisitorRegister();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	} // end method main
}

Recommended Answers

All 14 Replies

I can't make it work

Is this a java programming problem
or a database question?

If a java problem, please post the full text of the error message(s)
or explain what the programming problem is.

As I understand, we may use JDBC to do the job for MS Access. Java provides SQL(including updating) methods.

The problem lies in the following method. On clicking the Button the method should collect parameters and insert into the MS Database.

private void AppointmentJButtonActionPerformed(ActionEvent event) {
String title = (String) TitleJComboBox.getSelectedItem();
String firstName = FirstNameJTextField.getText();
String lastName = LastNameJTextField.getText();
String companyName = CompanyNameJTextField.getText();
String visitor = (String) VisitorTypeJComboBox.getSelectedItem();
String host = HostNameJTextField.getText();
String location = (String) HostLocationJComboBox.getSelectedItem();

VisitorDA.initialize();
Visitor newVisitor = VisitorDA.newVisitor(rs.getString("title"), rs.getString ("fname"), rs.getString ("lname"), rs.getString ("compname"), rs.getString ("vistype"), rs.getString ("hname"), rs.getString ("location"));

// Visitor newVisitor = VisitorDA.newVisitor(title, firstName, lastName, companyName, visitor, host, location);

VisitorDA.terminate();
}// end method AppointmentJButtonActionPerformed

the method should ...

You are not explaining what the problem is.
What does the method do?

Dear NormR1 as I tried to explain earlier its a Three tier structure with Problem Domain, Data Access and GUI classes along with an MS Access Database. If the user clicks on the Appointment Button it should take values from the form and insert into the database. In my case this is not happening and I get errors given below:

Initialising the DA tier
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] '(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Register.VisitorDA.initialize(VisitorDA.java:24)
at Register.Visitor.initialize(Visitor.java:36)
at Register.VisitorRegister.main(VisitorRegister.java:288)

Process completed.

/*
Staff.java
This class holds all information about Staff object
*/

package Register;

public class Visitor
{
	// attribute definitions
	/* All the fields are made strings to faciliate the construction of the SQL statement */
	String title;
	String firstName; // "camelCase"
	String lastName;
	String companyName;
	String visitorType;
	String hostName;
	String staffLocation;

	/* Creates a new instance of Staff constructor takes five strings as a parameters */
	public Visitor(String t, String fn, String ln, String cn, String vt, String hn, String loc)
		{
			title = t;
			firstName = fn;
			lastName = ln;
			companyName = cn;
			visitorType = vt;
			hostName = hn;
			staffLocation = loc;
		}

	public static Visitor newVisitor(String t, String fn, String ln, String cn, String vt, String hn, String loc)
		{
			return VisitorDA.newVisitor(t, fn, ln, cn, vt, hn, loc);
		}
	public static void initialize(){ VisitorDA.initialize(); }
	public static void terminate(){ VisitorDA.terminate(); }

	public String getTitle()
		{ return title; }
	public void setTitle(String t)
		{ title = t; }

	public String getFirstName()
		{ return firstName; }
	public void setFirstName(String fn)
		{ firstName = fn; }

	public String getLastName()
		{ return lastName; }
	public void setLastName(String ln)
		{ lastName = ln; }

	public String getCompanyName()
		{ return companyName; }
	public void setCompanyName(String cn)
		{ companyName = cn; }

	public String getVisitorType()
		{ return visitorType; }
	public void setVisitorType(String vt)
		{visitorType = vt; }

	public String getHostName()
		{ return hostName; }
	public void setHostName(String hn)
		{ hostName = hn; }

	public String getStaffLocation()
		{ return staffLocation; }
	public void setStaffLocation(String loc)
		{ staffLocation = loc; }

	/* Override the string object the strign method
	This returns a string representation of Staff object */
	public String printString()
	{
		return title + " " + firstName + " " + lastName + " " + companyName + " " + visitorType + " " + hostName + " " + staffLocation;
	}
}
/*
VisitorDA.java
This class has static fields and methods and connect to the access database
The initialize() method start the connection and terminate() method closes the connection
The rest of the method executes sql statements and perform updates to the database
*/

package Register;

import java.util.*;
import java.sql.*;

public class VisitorDA {
	//Static Fields
	private static Connection dbcon;
	private static  Visitor aVisitor;

	/* Initialise method. This method start the connection to the tennis club database added in the odbc bridge */
	public static void initialize()	{
		try {
			System.out.println("Initialising the DA tier");
			/* to use the jdbc-odbc bridge to MSAccess (via ODBC administrator):*/
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection dbcon = DriverManager.getConnection("jdbc:odbc:Register", "", "");
        }
        catch(Exception e) {
        	e.printStackTrace();
        }
    }

    /** This method closes the connection to the database*/
    public static void terminate() {
		try {
			dbcon.close();
		}
		catch (Exception e) { e.printStackTrace(); }
    }
    /* Add new Visitor to database
     This method receive the Visitor parameters and add it to he database Visitor table
     Create a Visitor type object and return the created Visitor object*/
    public static Visitor newVisitor(String t, String fn, String ln, String cn, String vt, String hn, String loc)
    {
		// Creates a new item Visitor and at the same time do database access to save the details
		Visitor vis = new Visitor(t, fn, ln, cn, vt, hn, loc);
		String cmd = null;
		try {
			Statement st = dbcon.createStatement();
			cmd = "insert into Visitor"
                + "(Title, First_Name, Last_Name, Company_Name, Visitor_Type, Host_Name, Staff_Location) values "
				+ "('" + t + "' , '" + fn + "' , '" + ln + "', '"+ cn +"', '"+ vt +"' , '" + hn + "' , '" + loc + "');";
			System.out.println(cmd);
            st.executeUpdate(cmd);
			st.close();
			dbcon.close();
			}
		catch (Exception e) { e.printStackTrace(); }
		finally {
            // Ensure the connection is closed (or it's a normal Connection)
            // or returned to the pool if it's a PooledConnection
            try{if (dbcon !=null) dbcon.close(); }
            catch(SQLException e){e.printStackTrace();}
        }
		return vis;
	}
} // end of class 'VisitorDA.java'
// GUI Visitor Register

package Register;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class VisitorRegister extends JFrame {

	// GUI variables
	// JLabel and JComboBox for Title
	private JLabel TitleJLabel;
	private JComboBox TitleJComboBox;

	// JLabel and JTextField for FirstName
	private JLabel FirstNameJLabel;
	private JTextField FirstNameJTextField;

	// JLabel and JTextField for LastName
	private JLabel LastNameJLabel;
	private JTextField LastNameJTextField;

	// JLabel and JTextField for CompanyName
	private JLabel CompanyNameJLabel;
	private JTextField CompanyNameJTextField;

	// JLabel and JComboBox for VisitorType
	private JLabel VisitorTypeJLabel;
	private JComboBox VisitorTypeJComboBox;

	// JLabel and JTextField for HostName
	private JLabel HostNameJLabel;
	private JTextField HostNameJTextField;

	// JLabel and JTextField for HostLocation
	private JLabel HostLocationJLabel;
	private JComboBox HostLocationJComboBox;

	// JButton for NewVisitor
	private JButton NewVisitorJButton;

	// JButton for Appointment
	private JButton AppointmentJButton;

	// JButton for CancelAppointment
	private JButton CancelAppointmentJButton;

	// JButton for StaffHost
	private JButton StaffHostJButton;

	// JButton for Login
	private JButton LoginJButton;

	// JButton for LogOff
	private JButton LogOffJButton;

	// no-argument constructor
	public VisitorRegister() {
		createUserInterface();
	}

	// create and position GUI components; register event handlers
	public void createUserInterface() {
		// get content pane and set layout to null
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		// set up TitleJLabel
		TitleJLabel = new JLabel();
		TitleJLabel.setText("Title: ");
		TitleJLabel.setBounds(15, 15, 30, 25);// done (x, y, w, h)
		contentPane.add(TitleJLabel);

		// set up TitleJComboBox
		String title[] = {"Dr", "Mr", "Mrs", "Ms"};
		TitleJComboBox = new JComboBox(title);
		TitleJComboBox.setBounds(50, 15, 50, 25);// done (x, y, w, h)
		contentPane.add(TitleJComboBox);

		// set up FirstNameJLabel
		FirstNameJLabel = new JLabel();
		FirstNameJLabel.setText("First Name: ");
		FirstNameJLabel.setBounds(105, 15, 70, 25);// done (x, y, w, h)
		contentPane.add(FirstNameJLabel);

		// set up FirstNameJTextField
		FirstNameJTextField = new JTextField();
		FirstNameJTextField.setText(" ");
		FirstNameJTextField.setBounds(180, 15, 125, 25);// done (x, y, w, h)
		contentPane.add(FirstNameJTextField);

		// set up LastNameJLabel
		LastNameJLabel = new JLabel();
		LastNameJLabel.setText("Last Name: ");
		LastNameJLabel.setBounds(320, 15, 70, 25);// done (x, y, w, h)
		contentPane.add(LastNameJLabel);

		// set up LastNameJTextField
		LastNameJTextField = new JTextField();
		LastNameJTextField.setText(" ");
		LastNameJTextField.setBounds(395, 15, 120, 25);// done (x, y, w, h)
		contentPane.add(LastNameJTextField);

		// set up CompanyNameJLabel
		CompanyNameJLabel = new JLabel();
		CompanyNameJLabel.setText("Company Name: ");
		CompanyNameJLabel.setBounds(15, 55, 100, 25);// done (x, y, w, h)
		contentPane.add(CompanyNameJLabel);

		// set up CompanyNameJTextField
		CompanyNameJTextField = new JTextField();
		CompanyNameJTextField.setText(" ");
		CompanyNameJTextField.setBounds(120, 55, 170, 25);// done (x, y, w, h)
		contentPane.add(CompanyNameJTextField);

		// set up VisitorTypeJLabel
		VisitorTypeJLabel = new JLabel();
		VisitorTypeJLabel.setText("Visitor Type: ");
		VisitorTypeJLabel.setBounds(315, 55, 85, 25);// done (x, y, w, h)
		contentPane.add(VisitorTypeJLabel);

		// set up VisitorTypeJComboBox
		String visitor[] = {"Cleaner", "Contractor", "Courier", "Staff", "Visitor"};
		VisitorTypeJComboBox = new JComboBox(visitor);
		VisitorTypeJComboBox.setBounds(395, 55, 120, 25);// done (x, y, w, h)
		contentPane.add(VisitorTypeJComboBox);

		// set up HostNameJLabel
		HostNameJLabel = new JLabel();
		HostNameJLabel.setText("Host's Full Name: ");
		HostNameJLabel.setBounds(15, 95, 100, 25);// done (x, y, w, h)
		contentPane.add(HostNameJLabel);

		// set up HostNameJTextField
		HostNameJTextField = new JTextField();
		HostNameJTextField.setText(" ");
		HostNameJTextField.setBounds(120, 95, 170, 25);// done (x, y, w, h)
		contentPane.add(HostNameJTextField);

		// set up HostLocationJLabel
		HostLocationJLabel = new JLabel();
		HostLocationJLabel.setText("Host Location: ");
		HostLocationJLabel.setBounds(305, 95, 85, 25);// done (x, y, w, h)
		contentPane.add(HostLocationJLabel);

		// set up HostLocationJComboBox
		String location[] = {"Ground Floor", "Lower Ground Floor", "1st Floor", "2nd Floor", "3rd Floor", "4th Floor", "5th Floor"};
		HostLocationJComboBox = new JComboBox(location);
		HostLocationJComboBox.setBounds(395, 95, 120, 25);// done (x, y, w, h)
		contentPane.add(HostLocationJComboBox);

		//set up NewVisitorJButton
		NewVisitorJButton = new JButton();
		NewVisitorJButton.setText("New Visitor");
		NewVisitorJButton.setBounds(35, 135, 150, 30); // done (x, y, w, h)
		contentPane.add(NewVisitorJButton);
        // anonymous inner class for ActionListener
        NewVisitorJButton.addActionListener(
                new ActionListener() {
                    // method called when calculate JButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        NewVisitorJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener

		// set up AppointmentJButton
		AppointmentJButton = new JButton();
		AppointmentJButton.setText("Make Appointment");
		AppointmentJButton.setBounds(190, 135, 150, 30);// done (x, y, w, h)
		contentPane.add(AppointmentJButton);
		// anonymous inner class for ActionListener
        AppointmentJButton.addActionListener(
                new ActionListener() {
                    // method called when calculate JButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        AppointmentJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener

		// set up CancelAppointmentJButton
		CancelAppointmentJButton = new JButton();
		CancelAppointmentJButton.setText("Cancel Appointment");
		CancelAppointmentJButton.setBounds(345, 135, 150, 30);// done (x, y, w, h)
		contentPane.add(CancelAppointmentJButton);
		// anonymous inner class for ActionListener
        CancelAppointmentJButton.addActionListener(
                new ActionListener() {
                    // method called when calculate JButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        CancelAppointmentJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener

		// set up LoginJButton
		LoginJButton = new JButton();
		LoginJButton.setText("Login Visitor");
		LoginJButton.setBounds(35, 175, 150, 30);// done (x, y, w, h)
		contentPane.add(LoginJButton);
		// anonymous inner class for ActionListener
        LoginJButton.addActionListener(
                new ActionListener() {
                    // method called when calculate JButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        LoginJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener

		// set up LogOffJButton
		LogOffJButton = new JButton();
		LogOffJButton.setText("Log Off Visitor");
		LogOffJButton.setBounds(190, 175, 150, 30);// done (x, y, w, h)
		contentPane.add(LogOffJButton);
		// anonymous inner class for ActionListener
        LogOffJButton.addActionListener(
                new ActionListener() {
                    // method called when calculate JButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        LogOffJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener

		// set up StaffHostJButton
		StaffHostJButton = new JButton();
		StaffHostJButton.setText("Add Staff / Host");
		StaffHostJButton.setBounds(345, 175, 150, 30);// done (x, y, w, h)
		contentPane.add(StaffHostJButton);
		// anonymous inner class for ActionListener
        StaffHostJButton.addActionListener(
                new ActionListener() {
                    // method called when StaffHostJButton is pressed
                    public void actionPerformed(ActionEvent event) {
                        StaffHostJButtonActionPerformed(event);
                    }
                } // end of anonymous inner class
                ); // end call to addActionListener


		// set properties of application’s window
		setTitle("Visitor's Register"); // set title bar text
		setSize(540, 250); // set window size
		setVisible(true); // display window
}// end method createUserInterface

	// Methods for ActionListeners
	private void NewVisitorJButtonActionPerformed(ActionEvent event) {
		FirstNameJTextField.setText(" ");
		LastNameJTextField.setText(" ");
		CompanyNameJTextField.setText(" ");
		HostNameJTextField.setText(" ");
	}

	private void AppointmentJButtonActionPerformed(ActionEvent event) {
		String title = (String) TitleJComboBox.getSelectedItem();
		String firstName = FirstNameJTextField.getText();
		String lastName = LastNameJTextField.getText();
		String companyName = CompanyNameJTextField.getText();
		String visitorType = (String) VisitorTypeJComboBox.getSelectedItem();
		String hostName = HostNameJTextField.getText();
		String hostLocation = (String) HostLocationJComboBox.getSelectedItem();

		VisitorDA.initialize();
		Visitor vis =  Visitor.newVisitor(title, firstName, lastName, companyName, visitorType, hostName, hostLocation);
		System.out.println("Created new Visitor: " + vis.printString());
		VisitorDA.terminate();
	}// end method AppointmentJButtonActionPerformed

	private void CancelAppointmentJButtonActionPerformed(ActionEvent event) {
	}

	private void LoginJButtonActionPerformed(ActionEvent event) {
	}

	private void LogOffJButtonActionPerformed(ActionEvent event) {
	}

	private void StaffHostJButtonActionPerformed(ActionEvent event) {
	}

	// main method
	public static void main(String[] args) {
		VisitorRegister application = new VisitorRegister();
		Visitor.initialize();
		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	} // end method main
}

Dear NormR1 I have now included all the files code for your convenience. Hope you would help me out. It compiles and runs well with no errors but once the user clicks on the AppointmentJButton the following lines of errors are generated:

--------------------Configuration: <Default>--------------------
Initialising the DA tier
Initialising the DA tier
java.lang.NullPointerException
at Register.VisitorDA.newVisitor(VisitorDA.java:47)
at Register.Visitor.newVisitor(Visitor.java:34)
at Register.VisitorRegister.AppointmentJButtonActionPerformed(VisitorRegister.java:268)
at Register.VisitorRegister.access$100(VisitorRegister.java:9)
at Register.VisitorRegister$2.actionPerformed(VisitorRegister.java:178)
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:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
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:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
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)
Created new Visitor: Dr tg gg gg Cleaner ggg Ground Floor
java.lang.NullPointerException
at Register.VisitorDA.terminate(VisitorDA.java:34)
at Register.VisitorRegister.AppointmentJButtonActionPerformed(VisitorRegister.java:270)
at Register.VisitorRegister.access$100(VisitorRegister.java:9)
at Register.VisitorRegister$2.actionPerformed(VisitorRegister.java:178)
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:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
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:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
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)

Process completed.

Can someone please help me out?

The error message says that there is an object reference variable at line 47 that has a null value. If this is line 47 in VisitorDA:
Statement st = dbcon.createStatement();
What is the value of dbcon when that line is executed?

Dear NormR1 this is the whole code of the class VisitorDA.java

/*
VisitorDA.java
This class has static fields and methods and connect to the access database
The initialize() method start the connection and terminate() method closes the connection
The rest of the method executes sql statements and perform updates to the database
*/

package Register;

import java.util.*;
import java.sql.*;

public class VisitorDA {
	//Static Fields
	private static Connection dbconn;
	private static Statement stmt;
	private static Visitor visitor;

	/* Initialise method. This method start the connection to the Visitor Database added in the odbc bridge */
	public static void initialize()	{
		Connection dbconn = null;
		try {
			System.out.println("Initialising the Data Access Tier");
			// to use the jdbc-odbc bridge to MSAccess (via ODBC administrator):
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// Create Connection Instance
			dbconn = DriverManager.getConnection("jdbc:odbc:Register", "", "");
			// Create Statement Object Instance for this Connection
			dbconn.createStatement();
        }
        catch(SQLException sqle) {
			System.err.println("SQLException: " + sqle);
		}
        catch(ClassNotFoundException cnfe) {
        	System.err.println("ClassNotFoundException: " + cnfe);
        	System.out.println("Unable to Load the Driver Class!");
        	cnfe.printStackTrace();
        }
    }

    /** This method closes the connection to the database*/
    public static void terminate() {
		try { // close everything
			stmt.close();
			dbconn.close();
		}
		catch (Exception e) { e.printStackTrace(); }
    }
    /* Add new Visitor to database
     This method receive the Visitor parameters and add it to he database Visitor table
     Create a Visitor type object and return the created Visitor object*/
    public static Visitor newVisitor(String t, String fn, String ln, String cn, String vt, String hn, String loc)
    {
		// Creates a new item Visitor and at the same time, do database access to save the details
		Visitor vis = new Visitor(t, fn, ln, cn, vt, hn, loc);
		String cmd = null;
		try {
			Statement stmt = dbconn.createStatement();
			cmd = "insert into Visitor"
                + "(Visitor_Title, First_Name, Last_Name, Company_Name, Visitor_Type, Host_Full_Name, Host_Location) values "
				+ "('" + t + "' , '" + fn + "' , '" + ln + "', '"+ cn +"', '"+ vt +"' , '" + hn + "' , '" + loc + "')";
			System.out.println(cmd);
            stmt.executeUpdate(cmd);
			stmt.close();
			dbconn.close();
			}
		catch (Exception e) { e.printStackTrace(); }
		finally {
            // Ensure the connection is closed (or it's a normal Connection)
            // or returned to the pool if it's a PooledConnection
		try{if (dbconn != null) dbconn.close(); }
		catch(SQLException e){e.printStackTrace();}
        }
		return vis;
	}
} // end of class 'VisitorDA.java'

You didn't answer my questions:
The error message says that there is an object reference variable at line 47 that has a null value. If this is line 47 in VisitorDA:
Statement st = dbcon.createStatement();
What is the value of dbcon when that line is executed?

I want you to look in your code and find your problem. The error message says where the problem is located. Look at that line and see why there is a NPE ocuring there.

Insert a line before the one with the problem to verify it:

System.out.println("dbcon = " + dbcon); // verify contents of dbcon

It's been resolved. Thanks.

/*
VisitorDA.java
This class has static fields and methods and connect to the access database
The initialize() method start the connection and terminate() method closes the connection
The rest of the method executes sql statements and perform updates to the database
*/

package Register;

import java.util.*;
import java.sql.*;

public class VisitorDA {
	//Static Fields
	private static Connection conn;


	/* Initialise method. This method start the connection to the Visitor Database added in the odbc bridge */
	public static void initialize()	{
		Connection dbconn = null;
		try {
			System.out.println("Initialising the Data Access Tier");
			// to use the jdbc-odbc bridge to MSAccess (via ODBC administrator):
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// Create Connection Instance
			conn = DriverManager.getConnection("jdbc:odbc:Register", "", "");
			// Create Statement Object Instance for this Connection
			conn.createStatement();
        }
        catch(SQLException sqle) {
			System.err.println("SQLException: " + sqle);
		}
        catch(ClassNotFoundException cnfe) {
        	System.err.println("ClassNotFoundException: " + cnfe);
        	System.out.println("Unable to Load the Driver Class!");
        	cnfe.printStackTrace();
        }
    }

    /** This method closes the connection to the database*/
    public static void terminate() {
		try { // close everything
			conn.close();
		}
		catch (Exception e) { e.printStackTrace(); }
    }
    /* Add new Visitor to database
     This method receive the Visitor parameters and add it to he database Visitor table
     Create a Visitor type object and return the created Visitor object*/
    public static Visitor newVisitor(String t, String fn, String ln, String cn, String vt, String hn, String loc)
    {
		// Creates a new item Visitor and at the same time, do database access to save the details
		Visitor vis = new Visitor(t, fn, ln, cn, vt, hn, loc);
		try {
			Statement st = conn.createStatement();
			String cmd = "insert into Visitor"
                + "(Visitor_Title, First_Name, Last_Name, Company_Name, Visitor_Type, Host_Full_Name, Host_Location) values "
				+ "('" + t + "' , '" + fn + "' , '" + ln + "', '"+ cn +"', '"+ vt +"' , '" + hn + "' , '" + loc + "')";
			System.out.println(cmd);
            st.executeUpdate(cmd);
			st.close();
			conn.close();
			}
		catch (Exception e) { e.printStackTrace(); }
		finally {
        // Ensure the connection is closed (or it's a normal Connection)
        // or returned to the pool if it's a PooledConnection
		try{if (conn != null) conn.close(); }
		catch(SQLException e){e.printStackTrace();}
        }
		return vis;
	}
} // end of class 'VisitorDA.java'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.