0

I'm having trouble understanding as to why this code will not work, below is a method from the code which I try to call a cell value on a JTable. When the row is clicked on, the code takes the row and uses that information from that row and is supposed to populate the JTextBoxes with the information from there. The debugger is not helping since its only pointing at the first getValueAt() and calling it a 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())

and here is the class itself it is calling

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                   

}

I honestly have no idea what to do. Please comment if you need more information

  • "calling it a NullPointerException." Are you sure that is not your issue? Maybe you could post the stack trace for the `NPE`. – bradimus Feb 22 '16 at 15:47

1 Answers1

2

Why are you using invokeLater(...)???

That method adds the code to the end of the Event Dispatch Thread, which means that it executed after all your setText() statements. Get rid of the invokeLater().

Also, where is the editPatientDialogBox variable initialized. Your code just invokes new EditPatientDialogBox() but doesn't assign the object created to any variable.

I would expect the code should be:

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);

Also, class names should start with an upper case character. Fix your class name.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • disagree with 1st. sentence, because invokeLater is used as an InitialThread ( moving pack() and setVisible() to the end of EDT queue) – mKorbel Feb 22 '16 at 17:40
  • 1
    That is why move the setVisible(...) statement to the end of the method. – camickr Feb 22 '16 at 18:11
  • I used a generator to create most of the initial GUI, so that might be why some things are pretty weird. – Jake Lalonde Feb 24 '16 at 12:48