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 7588095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:50:20+00:00 2026-05-30T19:50:20+00:00

I have a query. I have a database, and I am trying to write

  • 0

I have a query. I have a database, and I am trying to write code, so that a record in the database can be created from the java software.

I have a connector class that connects to the database, then a registerStudent class, that lets the user type in value into 2 textfields. then the values should be used to create a record in the database table.

when i hit the submit button it gives me this error code:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at RegisterStudent$2.actionPerformed(RegisterStudent.java:99)

FYI – Line 99 Code =

con.stmt.executeUpdate("INSERT INTO staff (Name, Profession)"+"VALUES"+"("+"'"+name+"',"+"'"+profession+"')");

This is my code for the registerStudent class:

import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;


public class RegisterStudent
{

    public RegisterStudent() {
        initialize();
    }

    public JFrame frmRegisterStudent;

    Connector con;
    private JTextField textField_1;
    private JTextField textField_2;

    // initialise the frame
    private void initialize() {
        frmRegisterStudent = new JFrame();
        frmRegisterStudent.setTitle("LEC AdminPro: RegisterStudents");
        frmRegisterStudent.setBounds(100, 100, 413, 225);
        frmRegisterStudent.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frmRegisterStudent.setLocationRelativeTo(null);


        Border border = LineBorder.createGrayLineBorder();
        frmRegisterStudent.getContentPane().setLayout(null);
        con = new Connector();

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(0, 0, 397, 189);
        frmRegisterStudent.getContentPane().add(panel_1);
        panel_1.setBorder(border);
        panel_1.setLayout(null);

        JLabel lblRegister = new JLabel("Register Student");
        lblRegister.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblRegister.setBounds(10, 11, 124, 20);
        panel_1.add(lblRegister);

        JLabel lblSurname = new JLabel("Name");
        lblSurname.setFont(new Font("Arial", Font.PLAIN, 11));
        lblSurname.setBounds(10, 63, 69, 14);
        panel_1.add(lblSurname);

        JLabel lblDob = new JLabel("Profession");
        lblDob.setFont(new Font("Arial", Font.PLAIN, 11));
        lblDob.setBounds(10, 88, 69, 14);
        panel_1.add(lblDob);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(104, 63, 266, 20);
        panel_1.add(textField_1);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(104, 88, 266, 20);
        panel_1.add(textField_2);


        JButton btnNewButton = new JButton("Cancel");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Object[] options = {"Yes", "No"};
                Component form = null;
                int n = JOptionPane.showOptionDialog(form, "Would you like to cancel the new Registration?", "Exit Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,  null, options, options);
                if(n == JOptionPane.YES_OPTION) {
                frmRegisterStudent.setVisible(false);
            }

        }
        });
        btnNewButton.setBounds(280, 119, 89, 23);
        panel_1.add(btnNewButton);

        JButton button = new JButton("Submit");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


            String name = textField_1.getText();    
            String profession = textField_1.getText();


                try {
            con.stmt.executeUpdate("INSERT INTO staff (Name, Profession)"+"VALUES"+"("+"'"+name+"',"+"'"+profession+"')");
            JOptionPane.showMessageDialog(frmRegisterStudent, "New Record has been added");

            } catch (SQLException e) {
            System.out.println("Record couldn't be added!");
            e.printStackTrace();
            }
            }
            });



        button.setBounds(181, 119, 89, 23);
        panel_1.add(button);


    }




    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                    try {
                RegisterStudent window = new RegisterStudent();
                window.frmRegisterStudent.setVisible(true);
                } catch (Exception e) {
                e.printStackTrace();
                }
            }
        });

        public void setVisible(boolean b) {
        frmRegisterStudent.setVisible(true);

}    
}
  • 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-05-30T19:50:21+00:00Added an answer on May 30, 2026 at 7:50 pm

    It looks like the problem is in the Connector class, which has a stmt field that does not get initialized.

    Use this instead of the line with the NullPointerException

    con.stmt = con.conn.prepareStatement("insert into staff (name, profession) values (?, ?)");
    con.stmt.setString(1, name);
    con.stmt.setString(2, profession);
    con.stmt.executeUpdate();
    

    But note that this is pretty bad design.

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

Sidebar

Related Questions

I have a form that shows results from a database query, these results can
I am trying to write a query that grabs information from one database and
I have a Microsoft Access database query that I'm trying to import into a
I'm trying to write a query for a database that will left join a
I'm trying to write my first query that uses a database join. I've gone
I am trying to write a custom code generator that can parse stored procedures.
I have a Django query and some Python code that I'm trying to optimize
I am trying to write a query for my database. Basically, I have an
I have a foxpro database that we are trying to write to using MS
I am trying to query a database. I already have a file that includes

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.