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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:41:41+00:00 2026-05-31T00:41:41+00:00

below I have got the code for the connector, then i have code for

  • 0

below I have got the code for the connector, then i have code for another class called viewHistory. now, what i was trying to do is to enable a user to enter a studentID number, and then the relevant attendance history should come up. It’s however compiloing but giving me an error message when i type in a number to check if it shows anything. Any suggestion?

public class Connector {
Connection con;
PreparedStatement stmt;
ResultSet rs;

Connector()
{
    try{


        Class.forName("com.mysql.jdbc.Driver");

        con=DriverManager.getConnection("jdbc:mysql://localhost/luxmidatabase","root","");
                    stmt=con.prepareStatement("select * from login where username=? and password=?");
                    }

    catch (Exception e) 
    {
        System.out.println(e);
    }
}
}

2nd CLASS

public class AttendanceHistory extends Connector
{

public AttendanceHistory() {
    initialize();
}

public JFrame frmAttendanceHistory;

Connector con;
private JTextField textField;

// initialise the frame
private void initialize() {
    frmAttendanceHistory = new JFrame();
    frmAttendanceHistory.setTitle("LEC AdminPro: Attendance History");
    frmAttendanceHistory.setBounds(100, 100, 323, 149);
    frmAttendanceHistory.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    frmAttendanceHistory.setLocationRelativeTo(null);
    // Initialising a Grey Border, which I am going to be using later to add borders to JLabels.
    Border border = LineBorder.createGrayLineBorder();
    frmAttendanceHistory.getContentPane().setLayout(null);
    con = new Connector();

    JPanel panel_1 = new JPanel();
    panel_1.setBounds(0, 0, 307, 113);
    frmAttendanceHistory.getContentPane().add(panel_1);
    panel_1.setBorder(border);
    panel_1.setLayout(null);

    JLabel lblRegister = new JLabel("View Attendance History");
    lblRegister.setFont(new Font("Tahoma", Font.BOLD, 12));
    lblRegister.setBounds(10, 11, 193, 20);
    panel_1.add(lblRegister);

    JLabel lblStudentId = new JLabel("Student ID");
    lblStudentId.setFont(new Font("Arial", Font.PLAIN, 11));
    lblStudentId.setBounds(10, 45, 69, 14);
    panel_1.add(lblStudentId);

    // Created a Cancel button along with an OptionDialog which pops up to confirm whether user wants to cancel registration or not.
    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 progress?", "Exit Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,  null, options, options);
            if(n == JOptionPane.YES_OPTION) {
                frmAttendanceHistory.setVisible(false);
            }

        }
    });
    btnNewButton.setBounds(159, 73, 75, 23);
    panel_1.add(btnNewButton);

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


        //String studentID = textField.getText();   

        try {


            con.stmt.executeQuery("SELECT StudentID, date FROM attendance WHERE StudentID = 21457");            
            while ( rs.next() ) {
                String date = rs.getString("date");
                System.out.println(date);
            }

            JOptionPane.showMessageDialog(frmAttendanceHistory, "Attendance has been registered.");
            frmAttendanceHistory.setVisible(false);

        } catch (SQLException e) {
        //System.out.println("Record couldn't be added!");
        JOptionPane.showMessageDialog(frmAttendanceHistory, "Attendance couldn't be registered. Please try again!");
        e.printStackTrace();
        }
        }
        });



    button.setBounds(81, 73, 75, 23);
    panel_1.add(button);

    textField = new JTextField();
    textField.setBounds(81, 42, 212, 20);
    panel_1.add(textField);
    textField.setColumns(10);




}




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

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



// method to make this frame visible (may be used in other classes for retrieval)
public void setVisible(boolean b) {
    frmAttendanceHistory.setVisible(true);

}    
}

Main Issue is the code inside the Submit Button. Thats where I try to filter the data.

This is the error Message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AttendanceHistory$2.actionPerformed(AttendanceHistory.java:85)
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$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.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)
  • 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-31T00:41:43+00:00Added an answer on May 31, 2026 at 12:41 am

    In the comments you said that the line where the error happens is

    while ( rs.next() ) {
    

    The NullPointerException happens because rs is null. I don’t see that you’re initializing rs anywhere in your code. Probably the line above should have been:

    ResultSet rs = con.stmt.executeQuery(...);
    

    (Where are you declaring the variable rs?).

    Another problem: You’re re-using the Statement object that you had prepared in the Connector object with the select * from login ... SQL. You should create a new statement to execute the query instead. Something like this:

    Statement st = con.con.createStatement();
    ResultSet rs = st.executeQuery("SELECT StudentID, date from ...");
    while (rs.hasNext()) {
        // ...
    }
    

    Note that you should also properly discard the Statement and ResultSet objects by calling close() on them. (Preferably in a finally block so that it happens even in case of an exception).

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

Sidebar

Related Questions

So far i have got the code below which works lovely when trying an
I am using VB.NET code. I have got the below string. http://localhost:3282/ISS/Training/SearchTrainerData.aspx Now I
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
I have got below code to GET dictionary type of session variable value. Please
I am using JQuery and I have got below JQuery Code sample. JQuery Code:
In my example code below, you can see that I have been trying to
Below I have a very simple example of what I'm trying to do. I
below i have a code that runs in most of my simple programs ..
I have below html code in my aspx. <input type=hidden id=medicalLink value='<a href=/forms/contactus.aspx >Contact
I have below query I am trying to show message 'No SubSource for this

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.