I am attempting to create a gui with two Jtables. One which outputs object Bike (if avaliable) and one which outputs object Rent. However Default Table Model keeps returning Null Point Exception. How do I populate a JTable with an arraylist of values then add it to a JPanel Grid Layout.
I have attempted to use Default Table Model and TableModel as well as search for the solution to not avail. This is for a basic GUI school project
Main Class where arrays are stored
public static final ArrayList<Bike> bikes = new ArrayList<>();
public static final ArrayList<Customer> customers = new ArrayList<>();
public static final ArrayList<Rent> rents = new ArrayList<>();
ArrayList<Rent> toRemove = new ArrayList<Rent>();
//Create variables
private int customerID = 0;
private final LocalDate currentDate = LocalDate.now();
//Main Method
public static void main(String args[]) {
//Create gui
GUI fr = new GUI();
fr.setSize(1000, 600);
fr.setVisible(true);
fr.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Create Initial Objects and fill Arrays
bikes.add(new Bike("Tarmac Disk Expert", 6000.00, "Mountain", true));
bikes.add(new Bike("Epic Hardtail Comp", 3500.00, "Mountain", true));
bikes.add(new Bike("Chisel Comp", 2000.00, "Road", true));
bikes.add(new Bike("Roubaix Sport", 3500.00, "Road", false));
bikes.add(new Bike("Turbo Levi Comp", 9500.00, "City", false));
bikes.add(new Bike("Venge Pro", 9400.00, "City", true));
bikes.add(new EBike("Turbo Como 2.0 650B", 4500.00, "Ebike", true, "Full Power"));
bikes.add(new EBike("Turbo Kenevo Expert", 9500.00, "Ebike", true, "Full Power"));
bikes.add(new EBike("Turbo Levo FSR", 5600.00, "Ebike", true, "Full Power"));
bikes.add(new EBike("Turbo Vado 4.0", 5600.00, "Ebike", true, "Power Assisted"));
bikes.add(new EBike("S-Works Turbo Levo", 4000.00, "Ebike", true, "Power Assisted"));
bikes.add(new EBike("Turbo Como 2.0 Low Entry", 6600.00, "Ebike", true, "Power Assisted"));
customers.add(new Customer(0001, "John Smith", true, "Roubaix Sport"));
customers.add(new Customer(0002, "Madamn Tuscoue", false, "N/A"));
customers.add(new Customer(0003, "James Lafroix", true, "Turbo Levi Comp"));
rents.add(new Rent(0001, "John Smith", true, "Roubaix Sport", LocalDate.of(2019, 03, 06), LocalDate.of(2019, 04, 05), 30, true));
rents.add(new Rent(0003, "James Lafroix", true, "Turbo Levi Comp", LocalDate.of(2019, 03, 20), LocalDate.of(2019, 04, 19), 30, false));
Bike Constructor
public Bike(String name, double price, String type, boolean available) {
this.name = name;
this.price = price;
this.type = type;
this.available = available;
}
Rent Constructor
public Rent(int customerID, String customerName, boolean renting, String bikeRented, LocalDate startDate, LocalDate endDate, int duration, boolean overdue) {
super(customerID, customerName, renting, bikeRented);
this.startDate = startDate;
this.endDate = endDate;
this.duration = duration;
this.overdue = overdue;
}
Lastly GUI class in which I am attempting to to use Default Table Model
public class GUI extends JFrame {
BikeNow controller = new BikeNow();
JPanel headingPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel saveDetailsPanel = new JPanel();
JPanel customerPanel = new JPanel();
JPanel bikePanel = new JPanel();
JTable bikesAvaliable, currentRents;
JButton btnaddCustomer, btnviewCustomer, btnaddRent, btnreturnRent, btnsaveFile, btnloadFile;
JLabel lblHeading = new JLabel("Welcome to Bike Now");
public GUI() {
super("Bike Now");
Font headingFont = new Font("Times New Roman", Font.BOLD, 60);
this.headingPanel.setLayout(new GridLayout(1, 1));
this.headingPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
lblHeading.setHorizontalAlignment(JLabel.CENTER);
lblHeading.setFont(headingFont);
this.headingPanel.add(this.lblHeading);
this.add(this.headingPanel, "North");
String[] columnNames1 = {"Bike Name", "Cost Per Day", "Bike Type"};
String[] columnNames2 = {"Start Date", "End Date", "Duration", "OverDue", "Customer ID", "Customer Name", "Renting", "Bike Rented"};
DefaultTableModel model = (DefaultTableModel)bikesAvaliable.getModel();
Object rowData[] = new Object[3];
for (int i = 0; i < controller.bikes.size(); i++) {
rowData[0] = controller.bikes.get(i).getName();
rowData[1] = controller.bikes.get(i).getPrice();
rowData[2] = controller.bikes.get(i).getType();
model.addRow(rowData);
}
bikesAvaliable.setModel(model);
//currentRents.setModel(tableModel2);
this.centerPanel.setLayout(new GridLayout(4, 1));
this.centerPanel.add(this.bikesAvaliable);
this.centerPanel.add(bikesAvaliable.getTableHeader());
//this.centerPanel.add(currentRents.getTableHeader());
//this.centerPanel.add(this.currentRents);
this.add(this.centerPanel, "Center");
}
}
NullPointError appearing at line
DefaultTableModel model = (DefaultTableModel)bikesAvaliable.getModel();
in Gui class
It looks like the bikesAvailable
variable isn't properly initialized. Just instantiate it with JTable bikesAvailable = new JTable();
or use another constructor: https://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html