【问题标题】:AWTQueue NullPointer [closed]AWTQueue NullPointer [关闭]
【发布时间】:2013-12-03 14:24:52
【问题描述】:

我正在尝试在哪里可以将类(客户)添加到 ArrayList,但在尝试添加客户时收到“AWT-EventQueue-0”java.lang.NullPointerException,有人知道为什么吗?

类如下

public void detailScreen(final int x) {
    //Creates a contentpane and sets the size.
    Container mainPanel = detailScreen.getContentPane();
    detailScreen.setPreferredSize(new Dimension(700, 500));

    //Creates a panel for buttons (if more a necessary)
    JPanel botPanel = new JPanel();
    JButton next = new JButton("Next");
    botPanel.add(next);

    //Creates a model for the table
    DefaultTableModel tableInfo = new DefaultTableModel() { 
        String col[] = {"First name", "Last name", "City", "Phone number"};

        @Override 
        public int getColumnCount() { 
            return 4;
        } 

        @Override
        public int getRowCount() {
            return x;
        }

        @Override 
        public String getColumnName(int index) { 
            return col[index]; 
        } 
    }; 

    //Creates the table, sets the row height and adds it to scrollpane
    final JTable table = new JTable(tableInfo);
    table.setRowHeight(35);
    JScrollPane scrollPane = new JScrollPane(table);

    //Adds the panels to the contentpane.
    mainPanel.add(scrollPane, "Center");
    mainPanel.add(botPanel, "South");

    //Adds an actionlistener to the button
    next.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            //Adds a new customer to an already instantiated ArrayList<Customer>();
            customer.add(new Customer(table.getValueAt(0, 0).toString(), table.getValueAt(0, 1).toString(), 10, 10, table.getValueAt(0, 3).toString()));

            detailScreen.setVisible(false);
            lastScreen();
        }
    });


    detailScreen.pack();
    detailScreen.setVisible(true);
}

【问题讨论】:

    标签: java swing arraylist nullpointerexception jtable


    【解决方案1】:

    你在哪里声明customer变量?

    你确定在访问之前实例化了吗?

    例如:

    List<Customer> customer = new ArrayList<>();
    

    【讨论】:

    • 我已经修好了。但是我在构造函数中实例化了它
    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2013-06-22
    • 2013-03-09
    • 2012-06-21
    • 1970-01-01
    相关资源
    最近更新 更多