This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                4年前关闭。
            
        

我无法理解为什么此代码不起作用,下面是我尝试在JTable上调用单元格值的代码中的方法。单击该行后,代码将使用该行并使用该行中的信息,并应使用该行中的信息填充JTextBox。调试器没有帮助,因为它仅指向第一个getValueAt()并将其称为NullPointerException。

private void editInfoButtonActionPerformed(java.awt.event.ActionEvent evt) {



    System.out.println("Opening 'Edit Info' window.");

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new editPatientDialogBox().setVisible(true);
        }


    });

    editPatientDialogBox.roomNumberField.setText(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),0).toString());
    editPatientDialogBox.nameField.setText(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),1).toString());
    editPatientDialogBox.genderField.setSelectedItem(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),2).toString());
    editPatientDialogBox.dateAdmittedField.setText(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),3).toString());
    editPatientDialogBox.ageField.setText(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),4).toString());
    editPatientDialogBox.isolationField.setSelectedItem(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),5).toString())


这是它正在调用的类本身

public class editPatientDialogBox extends javax.swing.JFrame {

public editPatientDialogBox() {
    initComponents();
}

@SuppressWarnings("unchecked")
private void initComponents() {

    roomNumberField = new javax.swing.JTextField();
    roomNumberLabel = new javax.swing.JLabel();
    nameField = new javax.swing.JTextField();
    nameLabel = new javax.swing.JLabel();
    genderField = new javax.swing.JComboBox();
    genderLabel = new javax.swing.JLabel();
    acceptButton = new javax.swing.JButton();
    isolationField = new javax.swing.JComboBox();
    isolationLabel = new javax.swing.JLabel();
    ageField = new javax.swing.JTextField();
    dateAdmittedField = new javax.swing.JTextField();
    cancelButton = new javax.swing.JButton();
    dateAdmittedLabel = new javax.swing.JLabel();
    ageLabel = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    roomNumberLabel.setText("Room Number");

    nameLabel.setText("Patient Name");

    genderField.setMaximumRowCount(2);
    genderField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Male", "Female" }));

    genderLabel.setText("Gender");

    acceptButton.setText("Accept");
    acceptButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            acceptButtonActionPerformed(evt);
        }
    });

    isolationField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Yes", "No" }));

    isolationLabel.setText("Isolation");

    ageField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            ageFieldActionPerformed(evt);
        }
    });

    dateAdmittedField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            dateAdmittedFieldActionPerformed(evt);
        }
    });

    cancelButton.setText("Cancel");
    cancelButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cancelButtonActionPerformed(evt);
        }
    });

    dateAdmittedLabel.setText("Date Admitted (YYYY/MM/DD)");

    ageLabel.setText("Date of Birth (YYYY/MM/DD)");


private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {

    if(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),bedboard.patientInfoTable.getSelectedRow()) == null)
    {
        System.out.println("None selected, try again.");
    }

    else
    {
    String get = null;

    info.setRoom(roomNumberField.getText());
    info.setPatientName(nameField.getText());
    info.setGender(genderField.getSelectedItem().toString());
    info.setDateAd(dateAdmittedField.getText());
    info.setAge(ageField.getText());
    info.setIso(isolationField.getSelectedItem().toString());

    setVisible(false);

    System.out.println("*********************************************************************************************");
    System.out.println("REVISED PATIENT");
    System.out.println("Patient Name: " + nameField.getText());
    System.out.println("Room Number: " + roomNumberField.getText());
    System.out.println("Gender: " + genderField.getSelectedItem());
    System.out.println("Date Admitted: " + dateAdmittedField.getText());
    System.out.println("Age: " + ageField.getText());
    System.out.println("Isolation: " + isolationField.getSelectedItem());
    System.out.println("*********************************************************************************************");


    bedboard.setValue(info.getRoom(get),bedboard.patientInfoTable.getSelectedRow(),0);
    bedboard.setValue(info.getPatientName(get),bedboard.patientInfoTable.getSelectedRow(),1);
    bedboard.setValue(info.getGender(get),bedboard.patientInfoTable.getSelectedRow(),2);
    bedboard.setValue(info.getDateAd(get),bedboard.patientInfoTable.getSelectedRow(),3);
    bedboard.setValue(info.getAge(get),bedboard.patientInfoTable.getSelectedRow(),4);
    bedboard.setValue(info.getIso(get),bedboard.patientInfoTable.getSelectedRow(),5);
    }
}

private void ageFieldActionPerformed(java.awt.event.ActionEvent evt) {

}

private void dateAdmittedFieldActionPerformed(java.awt.event.ActionEvent evt) {

}

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {

    System.out.println("Revise patient canceled.");
    setVisible(false);

}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(editPatientDialogBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(editPatientDialogBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(editPatientDialogBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(editPatientDialogBox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>



    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new editPatientDialogBox().setVisible(true);

        }
    });
}

// Variables declaration - do not modify
private javax.swing.JButton acceptButton;
public static javax.swing.JTextField ageField;
private javax.swing.JLabel ageLabel;
private javax.swing.JButton cancelButton;
public static javax.swing.JTextField dateAdmittedField;
private javax.swing.JLabel dateAdmittedLabel;
public static javax.swing.JComboBox genderField;
private javax.swing.JLabel genderLabel;
public static javax.swing.JComboBox isolationField;
private javax.swing.JLabel isolationLabel;
public static javax.swing.JTextField nameField;
private javax.swing.JLabel nameLabel;
public static javax.swing.JTextField roomNumberField;
private javax.swing.JLabel roomNumberLabel;

// End of variables declaration


}

老实说,我不知道该怎么办。如果您需要更多信息,请发表评论

最佳答案

为什么使用invokeLater(...) ???

该方法将代码添加到“事件调度线程”的末尾,这意味着它在所有setText()语句之后执行。摆脱invokeLater()。

另外,editPatientDialogBox变量在哪里初始化。您的代码仅调用new EditPatientDialogBox(),但不会将创建的对象分配给任何变量。

我希望代码应该是:

EditPatientDialogBox editPatiendDialogBox = new EditPatientDialogBox(); // note the capital "E" of the class name.
editPatientDialogBox.roomNumberField.setText(bedboard.patientInfoTable.getValueAt(bedboard.patientInfoTable.getSelectedRow(),0).toString());
...
editPatientDialogBox.setVisible(true);


另外,类名应以大写字母开头。修正您的班级名称。

07-24 15:48