首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JFormattedTextField nullpointerexception

JFormattedTextField nullpointerexception
EN

Stack Overflow用户
提问于 2013-04-02 06:22:43
回答 1查看 239关注 0票数 0

我有一个面板,其中显示了患者列表(ID/姓名/年龄)。我有一个jlist,当我单击患者时,它会将患者的数据链接到我面板中的文本字段。

问题:当我尝试更新患者信息时,只有在点击更新前没有单击年龄文本字段时,我才会收到年龄JFormattedTextField的nullpointerexception。

若要验证,请执行以下操作: 1.所有文本字段均为空2.我单击一个患者,系统会使用患者信息更新文本字段3.我将“患者ID”更改为其他值,然后单击“更新->空值”

但是,如果我单击患者,然后单击年龄JFTF,然后单击update,它可以很好地读取数据。

有没有办法“点击”文本字段??

我的代码=当我单击jlist

代码语言:javascript
复制
    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());

-

代码语言:javascript
复制
    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!");
    }
EN

回答 1

Stack Overflow用户

发布于 2013-04-10 05:52:03

我使用了错误的函数将年龄添加到年龄字段。因为它是格式化的,所以我必须使用setValue()。

旧代码

代码语言:javascript
复制
ageTF.setText(st.nextToken());

ageTF.setValue(Integer.valueOf(st.nextToken()));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15752970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档