Hi, I can't get my carrentalprogram to work. When I try add vehicle/customer, I get this:
"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"
I think it means that what i type in my textLines = Null.
I guess the methods from the class "Controller" can't be used.
Here are the classes "Controller" and "MainPage"
public class Controller {
CustomerRegister customers;
VehicleRegister vehicles;
BookingRegister bookings;
MainPage mainPage;
public void addCustomer(String customerName, String customerNo, String customerAdress, String customerTelNr){
Customer tmpCustomer = new Customer(customerName, customerNo, customerAdress, customerTelNr);
customers.addCustomer(tmpCustomer);
}
public Customer findCustomerByNo(String customerNo){
Customer tempCustomer = customers.findCustomerByNo(customerNo);
return tempCustomer;
}
public Customer findCustomerByName(String customerName){
Customer tempCustomer = customers.findCustomerByName(customerName);
return tempCustomer;
}
public void removeCustomer(String customerNo){
customers.removeCustomer(customerNo);
}
public void addBooking(String bookingNo, String date, Customer customer, Vehicle vehicle){
Booking tempBooking = new Booking(bookingNo, date, customer, vehicle);
bookings.addBooking(tempBooking);
}
public void removeBooking(String bookingNo){
bookings.removeBooking(bookingNo);
}
public void addVehicle(String type, String regNo, String location, String price, String NumberOfSeats){
Vehicle tempVehicle = new Vehicle(type, regNo, location, price, NumberOfSeats);
vehicles.addVehicle(tempVehicle);
}
public Vehicle findVehicle(String vehicleRegNo){
Vehicle tempVehicle = vehicles.findVehicle(vehicleRegNo);
return tempVehicle;
}
}
import java.awt.EventQueue;
public class MainPage {
private JPanel panelMainPage;
private JPanel panelCustomer;
private JPanel panelVehicle;
private JPanel panelBooking;
private JFrame frame;
private JTextField textFieldRegNo;
private JTextField textFieldPrice;
private JTextField textFieldNoOfSeats;
private JTextField textFieldLocation;
private JTextField textFieldType;
private JTextField textFieldName;
private JTextField textFieldCustomerNr;
private JTextField textFieldAdress;
private JTextField textFieldTelNo;
private JTextField textFieldBookingNo;
private JTextField textFieldDate;
private JTextField textFieldCustomerNo;
private JTextField textFieldRegNo_2;
Controller controller = new Controller();
Vehicle v1 = new Vehicle("aaa 111", "volvo", "Lund", "100kr", "5");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainPage window = new MainPage();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainPage() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel panelMainPage = new JPanel();
frame.getContentPane().add(panelMainPage, "name_98442922334630");
panelMainPage.setLayout(null);
panelMainPage.setVisible(true);
final JPanel panelBooking = new JPanel();
frame.getContentPane().add(panelBooking, "name_98496010255724");
panelBooking.setLayout(null);
panelBooking.setVisible(false);
final JPanel panelVehicle = new JPanel();
frame.getContentPane().add(panelVehicle, "name_98446560458481");
panelVehicle.setLayout(null);
panelVehicle.setVisible(false);
final JPanel panelCustomer = new JPanel();
frame.getContentPane().add(panelCustomer, "name_98449752211034");
panelCustomer.setLayout(null);
panelCustomer.setVisible(false);
JButton btnCustomer = new JButton("Customer");
btnCustomer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panelCustomer.setVisible(true);
panelMainPage.setVisible(false);
}
});
btnCustomer.setBounds(27, 54, 99, 41);
panelMainPage.add(btnCustomer);
JButton btnBooking = new JButton("Booking");
btnBooking.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panelBooking.setVisible(true);
panelMainPage.setVisible(false);
}
});
btnBooking.setBounds(174, 54, 99, 41);
panelMainPage.add(btnBooking);
JButton btnVehicle = new JButton("Vehicle");
btnVehicle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panelVehicle.setVisible(true);
panelMainPage.setVisible(false);
}
});
btnVehicle.setBounds(315, 54, 99, 41);
panelMainPage.add(btnVehicle);
JLabel lblRegNumber = new JLabel("Reg Number:");
lblRegNumber.setBounds(35, 33, 89, 14);
panelVehicle.add(lblRegNumber);
JLabel lblPrice = new JLabel("Price:");
lblPrice.setBounds(35, 77, 81, 14);
panelVehicle.add(lblPrice);
JLabel lblNoOfSeats = new JLabel("No of seats:");
lblNoOfSeats.setBounds(35, 123, 89, 14);
panelVehicle.add(lblNoOfSeats);
JLabel lblLocation = new JLabel("Location:");
lblLocation.setBounds(35, 165, 81, 14);
panelVehicle.add(lblLocation);
JLabel lblType = new JLabel("Type:");
lblType.setBounds(35, 207, 78, 14);
panelVehicle.add(lblType);
textFieldRegNo = new JTextField();
textFieldRegNo.setBounds(123, 30, 86, 20);
panelVehicle.add(textFieldRegNo);
textFieldRegNo.setColumns(10);
textFieldPrice = new JTextField();
textFieldPrice.setColumns(10);
textFieldPrice.setBounds(123, 74, 86, 20);
panelVehicle.add(textFieldPrice);
textFieldNoOfSeats = new JTextField();
textFieldNoOfSeats.setColumns(10);
textFieldNoOfSeats.setBounds(123, 120, 86, 20);
panelVehicle.add(textFieldNoOfSeats);
textFieldLocation = new JTextField();
textFieldLocation.setColumns(10);
textFieldLocation.setBounds(123, 162, 86, 20);
panelVehicle.add(textFieldLocation);
textFieldType = new JTextField();
textFieldType.setColumns(10);
textFieldType.setBounds(123, 204, 86, 20);
panelVehicle.add(textFieldType);
JButton btnAddVehicle = new JButton("Add vehicle");
btnAddVehicle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String VehicleRegNo = textFieldRegNo.getText();
String VehicleType = textFieldType.getText();
String VehicleNoOfSeats = textFieldNoOfSeats.getText();
String VehiclePrice = textFieldPrice.getText();
String VehicleLocation = textFieldLocation.getText();
controller.addVehicle(VehicleType, VehicleRegNo, VehicleLocation, VehiclePrice, VehicleNoOfSeats);
}
});
btnAddVehicle.setBounds(256, 73, 124, 23);
panelVehicle.add(btnAddVehicle);
JButton btnSearchVehicle = new JButton("Search vehicle");
btnSearchVehicle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// controller.findVehicle(vehicleRegNo)
}
});
btnSearchVehicle.setBounds(256, 29, 124, 23);
panelVehicle.add(btnSearchVehicle);
final JButton btmVHome = new JButton("Home");
btmVHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panelMainPage.setVisible(true);
panelVehicle.setVisible(false);
}
});
btmVHome.setBounds(0, -1, 89, 23);
panelVehicle.add(btmVHome);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBounds(256, 120, 138, 101);
panelVehicle.add(textArea_1);
JLabel lblNewLabel = new JLabel("Name");
lblNewLabel.setBounds(29, 38, 62, 14);
panelCustomer.add(lblNewLabel);
JLabel lblCustomerNo = new JLabel("Customer No");
lblCustomerNo.setBounds(29, 77, 86, 14);
panelCustomer.add(lblCustomerNo);
JLabel lblNewLabel_1 = new JLabel("Adress");
lblNewLabel_1.setBounds(29, 117, 86, 14);
panelCustomer.add(lblNewLabel_1);
JLabel lblTelNo = new JLabel("Tel No");
lblTelNo.setBounds(29, 160, 62, 14);
panelCustomer.add(lblTelNo);
textFieldName = new JTextField();
textFieldName.setBounds(106, 35, 86, 20);
panelCustomer.add(textFieldName);
textFieldName.setColumns(10);
textFieldCustomerNr = new JTextField();
textFieldCustomerNr.setBounds(106, 74, 86, 20);
panelCustomer.add(textFieldCustomerNr);
textFieldCustomerNr.setColumns(10);
textFieldAdress = new JTextField();
textFieldAdress.setBounds(106, 114, 86, 20);
panelCustomer.add(textFieldAdress);
textFieldAdress.setColumns(10);
textFieldTelNo = new JTextField();
textFieldTelNo.setBounds(106, 157, 86, 20);
panelCustomer.add(textFieldTelNo);
textFieldTelNo.setColumns(10);
JButton btnNewButton = new JButton("Search customer");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textFieldAdress.setText(controller.findCustomerByNo(textFieldCustomerNr.getText()).getAdress());
}
});
btnNewButton.setBounds(233, 34, 120, 23);
panelCustomer.add(btnNewButton);
JButton btnAddCustomer = new JButton("Add customer");
btnAddCustomer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String customerName = textFieldName.getText();
String customerNo = textFieldCustomerNr.getText();
String customerAdress = textFieldAdress.getText();
String customerTelNo = textFieldTelNo.getText();
controller.addCustomer(customerName, customerNo, customerAdress, customerTelNo);
}
});
btnAddCustomer.setBounds(233, 73, 120, 23);
panelCustomer.add(btnAddCustomer);
final JButton btmCHome = new JButton("Home");
btmCHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panelMainPage.setVisible(true);
panelCustomer.setVisible(false);
}
});
btmCHome.setBounds(0, 0, 89, 23);
panelCustomer.add(btmCHome);
final JTextArea textArea = new JTextArea();
textArea.setBounds(243, 112, 152, 116);
panelCustomer.add(textArea);
JLabel lblBookingNo_1 = new JLabel("Booking No");
lblBookingNo_1.setBounds(29, 41, 92, 14);
panelBooking.add(lblBookingNo_1);
JLabel lblBookingNo = new JLabel("Booking No");
lblBookingNo.setBounds(29, 41, 46, 14);
panelBooking.add(lblBookingNo);
JLabel lblDate = new JLabel("Date");
lblDate.setBounds(29, 139, 46, 14);
panelBooking.add(lblDate);
JLabel lblCustomerNo_1 = new JLabel("Customer No");
lblCustomerNo_1.setBounds(29, 189, 92, 14);
panelBooking.add(lblCustomerNo_1);
JLabel lblRegNo = new JLabel("Reg No");
lblRegNo.setBounds(29, 236, 46, 14);
panelBooking.add(lblRegNo);
textFieldBookingNo = new JTextField();
textFieldBookingNo.setBounds(130, 38, 86, 20);
panelBooking.add(textFieldBookingNo);
textFieldBookingNo.setColumns(10);
textFieldDate = new JTextField();
textFieldDate.setBounds(130, 136, 86, 20);
panelBooking.add(textFieldDate);
textFieldDate.setColumns(10);
textFieldCustomerNo = new JTextField();
textFieldCustomerNo.setBounds(131, 186, 86, 20);
panelBooking.add(textFieldCustomerNo);
textFieldCustomerNo.setColumns(10);
textFieldRegNo_2 = new JTextField();
textFieldRegNo_2.setBounds(130, 233, 86, 20);
panelBooking.add(textFieldRegNo_2);
textFieldRegNo_2.setColumns(10);
JButton btnAddBooking = new JButton("Add booking");
btnAddBooking.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String BookingNo = textFieldBookingNo.getText();
String Date = textFieldDate.getText();
String CustomerNo = textFieldCustomerNo.getText();
String RegNo = textFieldRegNo_2.getText();
// controller.addBooking(BookingNo, StartDate, EndDate,);
}
});
btnAddBooking.setBounds(259, 37, 116, 23);
panelBooking.add(btnAddBooking);
JButton btnSearchBooking = new JButton("Search booking");
btnSearchBooking.setBounds(259, 83, 116, 23);
panelBooking.add(btnSearchBooking);
JTextArea textArea_2 = new JTextArea();
textArea_2.setBounds(259, 134, 145, 116);
panelBooking.add(textArea_2);
final JButton btnBHome = new JButton("Home");
btnBHome.setToolTipText("MainPage");
btnBHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panelMainPage.setVisible(true);
panelBooking.setVisible(false);
}
});
btnBHome.setBounds(0, 0, 89, 23);
panelBooking.add(btnBHome);
}
}