Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Arraylist中的值填充Jtable(Jtables中的空指针异常)_Java_User Interface_Arraylist_Jtable_Tablemodel - Fatal编程技术网

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

Java 如何使用Arraylist中的值填充Jtable(Jtables中的空指针异常),java,user-interface,arraylist,jtable,tablemodel,Java,User Interface,Arraylist,Jtable,Tablemodel,我正在尝试创建一个包含两个JTable的gui。一个输出对象自行车(如果可用)和一个输出对象租金。但是,默认表模型始终返回空点异常。如何使用值的arraylist填充JTable,然后将其添加到JPanel网格布局中 我尝试使用默认表模型和表模型,并搜索解决方案,但没有效果。这是一个基本的GUI学校项目 存储数组的主类 public static final ArrayList<Bike> bikes = new ArrayList<>(); public sta

我正在尝试创建一个包含两个JTable的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;
    }
最后一个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");
    }

}

在Gui类中,
bikesavable
变量未正确初始化。只需使用
JTable bikesAvailable=new JTable()实例化它


或者使用另一个构造函数:

在GUI类中,BikesAvailable似乎没有为其分配对象。解决此问题的一种方法是在gui类中创建BikesAvailable对象,或者将在其他类中创建的BikesAvailable对象传递给gui类以便使用

谢谢大家!!这删除了空点异常。现在了解如何将arraylist数据添加到table@Chornologic
DefaultTableModel model = (DefaultTableModel)bikesAvaliable.getModel();