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)
In the comments you said that the line where the error happens is
The
NullPointerExceptionhappens becausersisnull. I don’t see that you’re initializingrsanywhere in your code. Probably the line above should have been:(Where are you declaring the variable
rs?).Another problem: You’re re-using the
Statementobject that you had prepared in theConnectorobject with theselect * from login ...SQL. You should create a new statement to execute the query instead. Something like this:Note that you should also properly discard the
StatementandResultSetobjects by callingclose()on them. (Preferably in afinallyblock so that it happens even in case of an exception).