gpt4 book ai didi

java - 在两个 GUI 类之间传输 String 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 23:35:18 24 4
gpt4 key购买 nike

我有一个表格,它是我的 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);

如何解决这个问题?

最佳答案

fNameTfAddUserDialog 构造函数中为 null。您没有初始化 fNameTf

编辑:

AddUserDialog userDialog = new AddUserDialog(this, userDialog.getfName(), userDialog.getlName(), userDialog.getGender());

userDialog.setfName(oldFName);

关于java - 在两个 GUI 类之间传输 String 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18803370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com