我有一个面板,其中显示了患者列表(ID/姓名/年龄)。我有一个jlist,当我单击患者时,它会将患者的数据链接到我面板中的文本字段。
问题:当我尝试更新患者信息时,只有在点击更新前没有单击年龄文本字段时,我才会收到年龄JFormattedTextField的nullpointerexception。
若要验证,请执行以下操作: 1.所有文本字段均为空2.我单击一个患者,系统会使用患者信息更新文本字段3.我将“患者ID”更改为其他值,然后单击“更新->空值”
但是,如果我单击患者,然后单击年龄JFTF,然后单击update,它可以很好地读取数据。
有没有办法“点击”文本字段??
我的代码=当我单击jlist
int patientIndex = patientList.getSelectedIndex();
if (patientIndex == -1) {
return;
}
Object info = listModel.get(patientIndex);
String infoString = (String) info;
StringTokenizer st = new StringTokenizer(infoString);
idTF.setText(st.nextToken());
if (idTF.getText().equals("Empty")) {
idTF.setText("");
return;
}
firstNameTF.setText(st.nextToken());
lastNameTF.setText(st.nextToken());
ageTF.setText(st.nextToken());-
String fName, lName, id, id2; // For now the ID will be name+age
int age;
Patient p = new Patient();
boolean gender;
// attempts to read the text fields
try {
fName = firstNameTF.getText();
lName = lastNameTF.getText();
id = idTF.getText();
age = ((Number) ageTF.getValue()).intValue();
System.out.println("age = " + age);
} catch (NullPointerException ex) {
System.out.println(ex.getMessage());
statusLabel.setText("All fields marked by a * are requried!");
}发布于 2013-04-10 05:52:03
我使用了错误的函数将年龄添加到年龄字段。因为它是格式化的,所以我必须使用setValue()。
旧代码
ageTF.setText(st.nextToken());
ageTF.setValue(Integer.valueOf(st.nextToken()));https://stackoverflow.com/questions/15752970
复制相似问题