当前位置: 首页 > 知识库问答 >
问题:

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

淳于兴朝
2023-03-14

我试图创建一个具有两个JTables的gui。一个输出对象自行车(如果可以的话)和一个输出对象出租。然而,缺省表模型总是返回空点异常。如何使用arraylist值填充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;
    }
 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();

在Gui类中

共有1个答案

金兴朝
2023-03-14

bikesAvaliable似乎没有在GUI类中为其分配对象。解决这个问题的一种方法是在gui类中创建您的bikesAvalible对象,或者将您在另一个类中创建的bikesAvaible对象传递给gui类,以便可以使用它。

 类似资料:
  • 作为一项任务,我被要求制作一个Swing GUI。在第一部分中,我创建了一个()对象()。目前所需要的是对所述数组列表进行排序,并将其显示为GUI中的。我制作了一个,当按下它时,它应该进行排序和显示。 但是,一旦按下它将只显示表标题而不显示数组列表中的信息,我应该做什么不同的事情呢?我已经在下面包括了GUI类和main类。

  • 问题内容: 我是Java新手,我有一个像这样的文本文件 我想用此文本文件中的数据填充“ jTable”。下面是到目前为止我的代码不起作用。当我执行程序时,表上没有任何显示。 有人可以帮帮我吗? 问题答案: 您需要将其更改为以下内容。每次读取新行时,都需要重置矢量,否则它包含第一行+第二行+以此类推。您还可以调用以避免初始行为空。并且您只需要添加行,您的注释[单元格包含很多列]的问题是由于 使用而引

  • 问题内容: 我有一个大查询表,看起来像这样:![Table [(https://ibb.co/1ZXMH71)如您所见,大多数值都是空的。我想向前填充这些空值,这意味着使用按时间排序的最后一个已知值。 显然,有一个名为FILL的函数但是我不知道如何使用它。 这是我尝试过发布到Web UI的查询: 我得到的错误是:语法错误:[3:6]处出现意外的标识符“ sns_6”,我想要的是获取一个新表,其中s

  • 它是从哪里来的?