简体   繁体   English

JFormattedTextField nullpointerException

[英]JFormattedTextField nullpointerexception

I have a panel that shows a list of patients (ID/names/age). 我有一个面板,显示患者列表(ID /名称/年龄)。 I have a jlist that when i click the patient it links the data of the patient to the textfields in my panel. 我有一个列表,当我单击患者时,它将患者的数据链接到面板中的文本字段。

The problem: When im trying to update patient information i get a nullpointerexception for my age JFormattedTextField, ONLY when i do not click the age text field before hitting update. 问题:当我尝试更新患者信息时,仅当我未点击年龄文本字段并单击更新之前,我就获得了我的年龄JFormattedTextField的nullpointerexception。

To verify 1. All text fields empty 2. i click a patient, it updates the textfields with the patient info 3. i change say patient ID to something different and click update -> nullpointerexception 验证1.所有文本字段为空2.我单击一个患者,它将使用患者信息更新文本字段3.我将患者ID更改为其他内容,然后单击更新-> nullpointerexception

but if i instead click the patient, and then just click the age JFTF and then hit update, it reads the data perfectly fine. 但是,如果我改为单击患者,然后单击年龄JFTF,然后单击update,它将读取数据完全正确。

Is there a way to "click" the textfield?? 有没有一种方法可以“单击”文本框?

my code = when i click the jlist 我的代码=当我单击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!");
    }

I was using the wrong function to add the age to the age field. 我使用了错误的函数将年龄添加到“年龄”字段中。 Since it is formatted, i must use setValue(). 由于已格式化,因此我必须使用setValue()。

old code 旧代码

ageTF.setText(st.nextToken());

ageTF.setValue(Integer.valueOf(st.nextToken()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM