在两个GUI类之间传递String时出现NullPointerException

时间:2013-09-14 15:33:25

标签: java swing

我有一张桌子,这是我的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);

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

fNameTfnull构造函数中为AddUserDialog。您尚未初始化fNameTf

编辑:

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

userDialog.setfName(oldFName);

答案 1 :(得分:1)

此行可以抛出NPE的唯一方法是fNameTf为空,因为它是该行上唯一的“取消引用”:

fNameTf.setText(oldFname);

如果在setfName调用中获得NPE,则可能意味着更新后的代码实际上并未运行 - 这是我体验中这种混淆的最常见原因。尝试执行清理然后构建。还要确保更新的代码是正在执行的代码(可能放在打印语句等中以证明它)。

    AddUserDialog userDialog = null;
    userDialog = new AddUserDialog(this, userDialog.getfName(), userDialog.getlName(), userDialog.getGender());
    userDialog.setfName(oldFName);  // Why is null here?!