我在arraylist中存储了一个名称,编号,电子邮件的对象(联系人)。 然后我想显示让我们说ScrollPane中的名称,但我得到一个错误检索它(来自我的其他类)。 我做错了什么?
下面是代码的这一部分(我在最后一行中使用了错误):
class GetUserInput implements ActionListener {
//It's the reactions to the buttons that are pressed
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Add Contact")) {
Agenda a = new Agenda();
System.out.println(event);
String inp = JOptionPane.showInputDialog("Enter your contact name");
a.setName(inp);
String inp1 = JOptionPane.showInputDialog("Enter your contact number");
a.setPhoneNumber(inp1);
String inp2 = JOptionPane.showInputDialog("Enter your contact email");
a.setEmail(inp2);
contacts.add(a);
} else if (event.getActionCommand().equals("Edit Contact")) {
String inp = JOptionPane.showInputDialog("Enter the name of the contact you wish to edit ");
} else if (event.getActionCommand().equals("Show All Contacts")) {
System.out.println(a.getName());
答案 0 :(得分:0)
您无法运行它,因为a
是在另一个块中创建的。在创建和分配新变量else if
之前,您无法在上一个a
块中访问它。