如何使用 Arraylist 中的值填充 Jtable(Jtables 中的空指针异常)
How to populate Jtable with values from Arraylist (Null Pointer Exception in Jtables)
我正在尝试用两个 Jtables 创建一个图形用户界面。一个输出对象 Bike(如果可用),一个输出对象 Rent。但是默认 Table 模型不断返回空点异常。如何使用值数组列表填充 JTable,然后将其添加到 JPanel 网格布局。
我尝试使用默认 Table 模型和 Table 模型,并搜索了无用的解决方案。这是一个基本的 GUI 学校项目
Main Class 存储数组的地方
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));
自行车制造商
public Bike(String name, double price, String type, boolean available) {
this.name = name;
this.price = price;
this.type = type;
this.available = available;
}
租建筑商
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;
}
最后是 GUI class,我试图在其中使用默认 Table 模型
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 出现在行
DefaultTableModel model = (DefaultTableModel)bikesAvaliable.getModel();
在桂class
bikesAvailable
变量似乎没有正确初始化。只需用 JTable bikesAvailable = new JTable();
实例化它
或使用另一个构造函数:https://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html
bikesAvaliable 似乎没有在 GUI 中分配给它的对象 class。解决这个问题的一种方法是在 gui class 中创建你的 bikesAvalible 对象,或者将你在另一个 class 中创建的 bikesAvaible 对象传递给 gui class 这样它就可以用过的。
我正在尝试用两个 Jtables 创建一个图形用户界面。一个输出对象 Bike(如果可用),一个输出对象 Rent。但是默认 Table 模型不断返回空点异常。如何使用值数组列表填充 JTable,然后将其添加到 JPanel 网格布局。
我尝试使用默认 Table 模型和 Table 模型,并搜索了无用的解决方案。这是一个基本的 GUI 学校项目
Main Class 存储数组的地方
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));
自行车制造商
public Bike(String name, double price, String type, boolean available) {
this.name = name;
this.price = price;
this.type = type;
this.available = available;
}
租建筑商
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;
}
最后是 GUI class,我试图在其中使用默认 Table 模型
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 出现在行
DefaultTableModel model = (DefaultTableModel)bikesAvaliable.getModel();
在桂class
bikesAvailable
变量似乎没有正确初始化。只需用 JTable bikesAvailable = new JTable();
或使用另一个构造函数:https://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html
bikesAvaliable 似乎没有在 GUI 中分配给它的对象 class。解决这个问题的一种方法是在 gui class 中创建你的 bikesAvalible 对象,或者将你在另一个 class 中创建的 bikesAvaible 对象传递给 gui class 这样它就可以用过的。