我有一张桌子,这是我的editButton
行动方法:
public void editAction() {
if (table.getSelectedRow() > -1) {
String oldFName = table.getValueAt(table.getSelectedRow(), 1).toString();
String oldLName = table.getValueAt(table.getSelectedRow(), 2).toString();
String oldGender = table.getValueAt(table.getSelectedRow(), 3).toString();
System.out.println(oldFName); // prints correctly
System.out.println(oldLName); // prints correctly
System.out.println(oldGender); // prints correctly
AddUserDialog userDialog = new AddUserDialog(this, oldFName, oldLName, oldGender);
} else JOptionPane.showMessageDialog(null, "Select A Row");
}
我的 AddUserDialog 类:
public AddUserDialog(JFrame owner, String oldFname, String oldLname, String oldGender) {
super(owner, "Edit User Information", true);
fNameTf.setText(oldFname); // First name text field
lNameTf.setText(oldLname); // Last name text field
if (oldGender.equals("Male")) maleRb.setSelected(true); //Male radio Button
else femaleRb.setSelected(true); //Female radio Button
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocation(400, 100);
setVisible(true);
pack();
}
...
}
现在,当我选择一行并单击编辑按钮时,旧的名字和旧的姓氏和旧的性别会在我的控制台上正确打印,但是我的 IDE 说这里有异常:
fNameTf.setText(oldFname); // why is Null here?!
更新 第二种方式:
public void editAction() {
if (table.getSelectedRow() > -1) {
String oldFName = table.getValueAt(table.getSelectedRow(), 1).toString();
String oldLName = table.getValueAt(table.getSelectedRow(), 2).toString();
String oldGender = table.getValueAt(table.getSelectedRow(), 3).toString();
System.out.println(oldFName); // prints correctly
System.out.println(oldLName); // prints correctly
System.out.println(oldGender); // prints correctly
AddUserDialog userDialog = null;
userDialog.setfName(oldFName); // Why is null here?!
userDialog.setlName(oldLName);
userDialog.setGender(oldGender);
userDialog = new AddUserDialog(this, userDialog.getfName(), userDialog.getlName(), userDialog.getGender());
} else JOptionPane.showMessageDialog(null, "Select A Row");
}
现在 NullPointerException 在这里:
userDialog.setfName(oldFName);
如何解决这个问题?