如何用Arraylist中的值填充Jtable(Jtables中的空指针异常)

时间:2019-05-25 14:01:30

标签: java user-interface arraylist jtable tablemodel

我试图用两个Jtables创建一个gui。一种输出对象Bike(如果有),另一种输出对象Rent。但是默认表模型不断返回空点异常。如何用值的数组列表填充JTable,然后将其添加到JPanel网格布局中。

我尝试使用默认表模型和表模型,以及搜索无效的解决方案。这是针对基本的GUI学校项目

存储数组的主类

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类

 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();

在桂班

2 个答案:

答案 0 :(得分:1)

看来,bikesAvaliable在GUI类中没有分配任何对象。解决此问题的一种方法是,在gui类中创建您的bikesAvalible对象,或将在另一个类中创建的bikesAvaible对象传递给gui类,以便可以使用它。

答案 1 :(得分:0)

似乎bikesAvailable变量未正确初始化。只需使用JTable bikesAvailable = new JTable();

实例化它

或使用其他构造函数:https://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html