我正在尝试将一个类(Customer)添加到ArrayList,但是当我尝试添加客户时,我得到一个“AWT-EventQueue-0”java.lang.NullPointerException,任何人都知道为什么?< / p>
课程如下
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);
}
答案 0 :(得分:1)
您在哪里声明customer
变量?
您确定在访问之前实例化了吗?
例如:
List<Customer> customer = new ArrayList<>();