gpt4 book ai didi

java - 保存客人详细信息时酒店预订系统运行时出错

转载 作者:行者123 更新时间:2023-12-02 06:19:29 25 4
gpt4 key购买 nike

我正在设计一个 Java 酒店预订系统作为学校项目。当我尝试保存客人的详细信息时,程序给出运行时错误(线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常)。我该如何解决这个问题?

提前非常感谢。

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

public class GuestDetailsForm extends JFrame implements ActionListener{
JTextField guestId = new JTextField(20);
JTextField name = new JTextField(20);
JTextField surname = new JTextField(20);
JTextField mobileNo = new JTextField(20);
JTextField email = new JTextField(20);
JTextField passportNo = new JTextField(20);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JButton insert = new JButton("Insert");
JButton cancel = new JButton("Cancel");
MainMenu mainMenu;

// This constructor will create a panel that allows the user to input the clients details
public GuestDetailsForm() {
this.setTitle("Guest Details");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setVisible(true);
this.setSize(500,300);

p1.setLayout(new FlowLayout());
p1.add(new JLabel("Guest Details"));

p2.setLayout(new GridLayout(6,2));
p2.add(new JLabel("Guest ID:", JLabel.LEFT));
p2.add(guestId);
guestId.setEditable(false);
p2.add(new JLabel("Name:", JLabel.LEFT));
p2.add(name);
p2.add(new JLabel("Surname:", JLabel.LEFT));
p2.add(surname);
p2.add(new JLabel("Mobile No:", JLabel.LEFT));
p2.add(mobileNo);
p2.add(new JLabel("Email:", JLabel.LEFT));
p2.add(email);
p2.add(new JLabel("Passport No:", JLabel.LEFT));
p2.add(passportNo);

try {
guestId.setText((mainMenu.getGuests().get(mainMenu.getGuests().size() - 1).getId() + 1) + "");
} catch(Exception e) {
guestId.setText("1");
}

p3.setLayout(new FlowLayout());
p3.add(insert);
p3.add(cancel);
insert.addActionListener(this);
cancel.addActionListener(this);

this.add(p1, BorderLayout.NORTH);
this.add(p2, BorderLayout.CENTER);
this.add(p3, BorderLayout.SOUTH);
}

// This method will clear all the fields
public void clearFields() {
name.setText("");
surname.setText("");
mobileNo.setText("");
email.setText("");
passportNo.setText("");

try {
guestId.setText((mainMenu.getGuests().get(mainMenu.getGuests().size() - 1).getId() + 1) + "");
} catch(Exception e){
guestId.setText("1");
}
}

// This method checks that none of the fields are empty
private boolean validation() {
if (name.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "Name cannot be empty", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}

if (surname.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "Surname cannot be empty", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}

if (mobileNo.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "Mobile number cannot be empty", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}

if (email.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "Email cannot be empty", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}

if (passportNo.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "Passport number cannot be empty", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}

return true;
}

@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if (btn == insert) {
if (validation() == true) {
MyGuests newGuest = new MyGuests(Integer.parseInt(guestId.getText()),
name.getText(),
surname.getText(),
mobileNo.getText(),
email.getText(),
passportNo.getText());

mainMenu.getGuests().add(newGuest);

JOptionPane.showConfirmDialog(this, "Guest has been successfully added!", "Message", JOptionPane.PLAIN_MESSAGE);

clearFields();
}
}
if (btn == cancel) {
this.dispose();
Guests guests = new Guests();
}
}

public static void main(String args[]) {
new GuestDetailsForm();
}
}

主菜单类

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

public class MainMenu extends JFrame implements ActionListener, WindowListener {
String FILE_PATH = "HotelReservationSystem.hrs";
JButton guest = new JButton("Guests");
JButton reservation = new JButton("Reservations");
JButton bill = new JButton("Bill");
JButton exit = new JButton("Exit");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
ArrayList<MyGuests> guests = new ArrayList<MyGuests>();
ArrayList<MyReservations> reservations = new ArrayList<MyReservations>();
ArrayList<MyRooms> rooms = new ArrayList<MyRooms>();

// This constructor creates the main menu of the program
public MainMenu() {
this.setTitle("Hotel Reservation System");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setVisible(true);
this.setSize(500,300);

p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));

p1.add(new JLabel("Welcome to the Panorama's Hotel Reservation System"));
p1.add(Box.createVerticalStrut(20));
p1.add(guest);
p1.add(Box.createVerticalStrut(20));
p1.add(reservation);
p1.add(Box.createVerticalStrut(20));
p1.add(bill);
guest.addActionListener(this);
reservation.addActionListener(this);
bill.addActionListener(this);

p2.setLayout(new FlowLayout());

p2.add(exit);
exit.addActionListener(this);

this.add(p1, BorderLayout.NORTH);
this.add(p2, BorderLayout.SOUTH);
}

// Creates a method to return the values that are stored in the MyGuests arraylist
public ArrayList<MyGuests> getGuests() {
return guests;
}

// Creates a method to return the values that are stored in the MyReservations arraylist
public ArrayList<MyReservations> getReservations() {
return reservations;
}

// Creates a method to return the values that are stored in the MyRooms arraylist
public ArrayList<MyRooms> getRooms() {
return rooms;
}

// Creates a method that searches the guest details by id
public MyGuests searchGuestById(int searchId) {
for (int i = 0; i < guests.size(); i++) {
if (guests.get(i).getId() == searchId) {
return guests.get(i);
}
}
return null;
}

// Creates a method that searches the reservation details by id
public MyReservations searchReservationById(String searchId) {
for (int i = 0; i < reservations.size(); i++) {
if (reservations.get(i).getId().equals(searchId)) {
return reservations.get(i);
}
}
return null;
}

// Creates a method that searches the reservation details by room number
public MyReservations searchReservationByRoom(String searchRoomNo) {
for (int i = 0; i < reservations.size(); i++) {
if (reservations.get(i).getRoomNo().equals(searchRoomNo)) {
return reservations.get(i);
}
}
return null;
}

// Load all data from file
public void windowOpened(WindowEvent e) {
ToSave save = new ToSave();
save.load(FILE_PATH);
guests = save.getGuests();
reservations = save.getReservations();
rooms = save.getRooms();
}

// Save all data to file
public void windowClosing(WindowEvent e) {
ToSave save = new ToSave();
save.setGuests(guests);
save.setReservations(reservations);
save.setRooms(rooms);
save.save(FILE_PATH);
}

public void windowClosed(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {
}

public void windowDeiconified(WindowEvent e) {
}

public void windowActivated(WindowEvent e) {
}

public void windowDeactivated(WindowEvent e) {
}

@Override
public void actionPerformed(ActionEvent e) {
JButton clicker = (JButton) e.getSource();
if (clicker == guest) {
this.dispose();
Guests guests = new Guests();
}
if (clicker == reservation) {
this.dispose();
Reservations reservations = new Reservations();
}
if (clicker == bill) {
this.dispose();
}
if (clicker == exit) {
System.exit(0);
}
}

public static void main(String args[]) {
new MainMenu();
}
}

最佳答案

您尚未实例化 MainMenu 对象,因此当您尝试向其中添加 guest 时所引用的对象尚不存在。

添加

mainMenu = new MainMenu();

在构造函数中

关于java - 保存客人详细信息时酒店预订系统运行时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21141529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com