猿问

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

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


我曾尝试使用默认表模型和 TableModel 以及搜索无用的解决方案。这是一个基本的 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"));


神不在的星期二
浏览 142回答 2
2回答

眼眸繁星

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

叮当猫咪

看起来bikesAvailable变量没有正确初始化。只需实例化它JTable bikesAvailable = new JTable();或使用另一个构造函数:https ://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html
随时随地看视频慕课网APP

相关分类

Java
我要回答