I'm trying to create a GUI for my console application but I run into an error when trying to use the print button which I cannot understand why.
This is the Code
package com.company;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.*;
public class WestminsterSkinConsultationManagerGUI extends JFrame {
private static WestminsterSkinConsultationManager manager;
private final ArrayList<Doctor> doctors;
private JTextField nameField;
private JTextField surnameField;
private JTextField dobField;
private JTextField mobileField;
private JTextField licenseField;
private JTextField specializationField;
private JTextField deleteField;
private JButton addButton;
private JButton deleteButton;
private JButton printButton;
private JButton saveButton;
private JButton exitButton;
private JTextArea doctorListArea;
public WestminsterSkinConsultationManagerGUI() {
// Initialize the manager
manager = new WestminsterSkinConsultationManager();
this.doctors = new ArrayList<Doctor>();
// Set up the frame
setTitle("Westminster Skin Consultation Manager");
setSize(600, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create the panel for the input fields
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(7, 2));
// Create the input fields
nameField = new JTextField();
surnameField = new JTextField();
dobField = new JTextField();
mobileField = new JTextField();
licenseField = new JTextField();
specializationField = new JTextField();
deleteField = new JTextField();
// Add the input fields to the panel
inputPanel.add(new JLabel("Name:"));
inputPanel.add(nameField);
inputPanel.add(new JLabel("Surname:"));
inputPanel.add(surnameField);
inputPanel.add(new JLabel("Date of birth:"));
inputPanel.add(dobField);
inputPanel.add(new JLabel("Mobile number:"));
inputPanel.add(mobileField);
inputPanel.add(new JLabel("Medical license number:"));
inputPanel.add(licenseField);
inputPanel.add(new JLabel("Specialization:"));
inputPanel.add(specializationField);
inputPanel.add(new JLabel("Medical license number to delete:"));
inputPanel.add(deleteField);
// Create the panel for the buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 1));
// Create the buttons
addButton = new JButton("Add Doctor");
deleteButton = new JButton("Delete Doctor");
printButton = new JButton("Print Doctors");
saveButton = new JButton("Save to File");
exitButton = new JButton("Exit");
// Add the buttons to the panel
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(printButton);
buttonPanel.add(saveButton);
buttonPanel.add(exitButton);
// Create the panel for the doctor list
JPanel listPanel = new JPanel();
doctorListArea = new JTextArea();
doctorListArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(doctorListArea);
listPanel.add(scrollPane);
// Add the input panel, button panel, and list panel to the frame
add(inputPanel, BorderLayout.WEST);
add(buttonPanel, BorderLayout.EAST);
add(listPanel, BorderLayout.SOUTH);
// Add action listeners to the buttons
addButton.addActionListener(new AddButtonListener());
deleteButton.addActionListener(new DeleteButtonListener());
printButton.addActionListener(new PrintButtonListener());
saveButton.addActionListener(new SaveButtonListener());
exitButton.addActionListener(new ExitButtonListener());
// Display the frame
setVisible(true);
}
// Inner class for the Add button listener
private class AddButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Get the input values
String name = nameField.getText();
String surname = surnameField.getText();
String dobString = dobField.getText();
String mobile = mobileField.getText();
String license = licenseField.getText();
String specialization = specializationField.getText();
// Create a new doctor object with the input values
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dob = null;
try {
dob = df.parse(dobString);
} catch (ParseException ex) {
JOptionPane.showMessageDialog(null, "Error: Invalid date of birth format. Use dd/MM/yyyy.");
return;
}
Doctor doctor = new Doctor(name, surname, dob, mobile, license, specialization);
// Add the doctor to the manager
manager.addDoctor(doctor);
}
}
// Inner class for the Delete button listener
private class DeleteButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Get the medical license number to delete
String license = deleteField.getText();
// Delete the doctor with the specified medical license number
manager.deleteDoctor(license);
}
}
// Inner class for the Print button listener
private class PrintButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Clear the text area
doctorListArea.setText("");
// Print the list of doctors
List<Doctor> doctors = manager.getDoctors();
for (Doctor doctor : doctors) {
doctorListArea.append("Name: " + doctor.getName() + "\n");
doctorListArea.append("Surname: " + doctor.getSurname() + "\n");
doctorListArea.append("Date of birth: " + doctor.getDateOfBirth() + "\n");
doctorListArea.append("Mobile number: " + doctor.getMobileNumber() + "\n");
doctorListArea.append("Medical license number: " + doctor.getMedicalLicenceNumber() + "\n");
doctorListArea.append("Specialization: " + doctor.getSpecialisation() + "\n\n");
}
}
}
// Inner class for the Save button listener
private static class SaveButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Save the list of doctors to a file
manager.saveToFile();
}
}
// Inner class for the Exit button listener
private static class ExitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Close the program
System.exit(0);
}
}
public static void main(String[] args) {
new WestminsterSkinConsultationManagerGUI();
}
}
This is the error I Get
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because "doctors" is null at com.company.WestminsterSkinConsultationManagerGUI$PrintButtonListener.actionPerformed(WestminsterSkinConsultationManagerGUI.java:158) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6616) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3398) at java.desktop/java.awt.Component.processEvent(Component.java:6381) at java.desktop/java.awt.Container.processEvent(Container.java:2266) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4991) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)