- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在设计一个 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/
我正在为一家小型酒店开发预订系统。我非常擅长 PHP,但不太擅长 SQL...我制作了一个表格,您可以在其中输入您的信息、房间数量并使用日历选择到达日期和退房日期。 现在一切都很顺利,直到我不得不检查
我有一个租车脚本。我把汽车放在汽车 table 上。我将所有预订信息保存在预订表中。预订表有开始日期和结束日期字段以及 car_id 字段。 例如:car_id: 14, from="2014-06-
我最近被问及一个关于假设的基于 Web 的预订系统的面试问题,以及我将如何设计数据库架构以最大限度地减少重复并最大限度地提高灵 active 。 用例是管理员将属性的可用性输入系统。可以设置多个时间段
如何在 中使用标签式标题预订 像 RMarkdown 一样? 在 RMarkdown 中: # heading1 {.tabset} ## tab1 content1 ## tab2 content2
我使用 Woothemes booking对于我的 woocommerce 网站 当我为产品建立自定义搜索时,我还必须搜索可用性,这是我应该如何管理来自 B.O 的可用性以及插件如何存储到数据库 a:
我正在尝试找到一种在亚马逊中按 ISBN 查找书名的简单方法。我正在协助我 child 的学校做一个简单的 list 。我的测试集中的很多书籍都无法使用其他来源(尝试使用 Google、WorldCa
我正在使用 JQuery-Mobile 在浏览器中开发一个房间预订应用程序。最初,我将预订建模为带有时间戳的记录和带有持续时间的字段。 现在,如果我有一个表/div,每个表代表一天的 15 分钟,那么
我有一个自托管 NancyFX Web 服务器的桌面应用程序。作为一个桌面应用程序,我们需要允许动态 IP 地址,因此我们使用 netsh 的通配符选项注册了 url,如下所示: netsh http
我有一个关于 x++ 和循环内容的问题要问。首先,这是代码: class MixFor5 { public static void main(String[] args) { int x = 0;
我在为带有等候名单的预订类型系统设计算法时遇到困难。 在任何给定时间都可以有 3 个活跃插槽,每个插槽持续一个月,之后它们将被放入等待列表中。我希望能够插入项目从等待状态转为事件状态的日期。 我知道三
我正在制作一个 session 室预订系统,您可以在其中选择特定的时间段,然后您应该在那时获得可用的房间。一些表的名称是法语,因此我将在需要时提供 () 之间的含义。我的问题是,当我选择时间时,我什至
我开始了一个新项目: 我只运行以下命令: rails g model subscription 然后: rails g controller subscriptions 然后: 已将resources
我正在通过一个 api 发布一个现场预订请求。当我通过一个表单操作发布数据并提交时,我会得到回复。但是当我试图通过 curl 发布相同的 url 以获得响应时,我没有得到任何数据。 网址是:- htt
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的非现场资源的问题对于 Stack Overflow 来说是无关紧要的
我想知道有用的资源/书籍,它们将有助于初学者学习网络服务器、应用程序服务器和网络架构基础知识。 我正在尝试了解不同的服务器如何相互交互。浏览器如何向服务器发送请求以及服务器如何发送响应。我有兴趣了解这
我正在开发一个项目,用户可以在餐厅进行预订,但仅限于餐厅允许的日期。餐厅还可以设置特定时间有多少个座位。 因此,餐厅可以提供每周格式的时间表,但也可以提供异常(exception)情况(例如假期)。
我正在尝试为以下计划改进 SQL: 看起来像这样: - @users.each do |user| - @dates.each do |date| %td - Booking.
我正在使用 WooCommerce 预订插件,我目前希望在预订摘要(产品选项)中显示其他信息。 为此,我使用了以下钩子(Hook):woocommerce_admin_booking_data_aft
请让我了解保留所有 ec2 实例时 ssd 的费用,那么为什么每 GB 月会扣除 0.10 美元? 我已经保留了 c4.2xlarge 和 m4.xlarge 实例,但仍然会从账单中连续扣除费用,仅针
我想知道是否有办法使用 Woocommerce 预订自动设置可预订产品的开始日期和结束日期。我想要实现的是,当用户进入我的产品页面时,开始日期和结束日期(月、日和年)将根据我希望通过 Jquery 设
我是一名优秀的程序员,十分优秀!