0

hoping someone can help with this as it's frying my brain now. I am new to RMI so this could be a basic amateurish mistake .

So I have a layered system where a userinterface creates an interface for an application layer and calls a getRecord function using RMI. This in turn creates an interface to a datalayer which retrieves the information from a database returning it to the appLayer which returns it to the userinterface. This works fine and the JTextFields are updated perfectly.

The problem is when i then try to send this information back through the applayer to another interface. When it gets to the new interface i can output the information to the console but it WILL NOT update the JTextFields on this interface and i cannot for the life of me understand why not.

My Main Interface:

IPatientRecord testRecord;
IAppLayer appLayer;

public class MainOffice extends JFrame {

private JPanel MainPanel;
private JPanel Background;
private JPanel TopPanel;
private JPanel LeftPanel;
private JPanel RightPanel;
private JTextField searchTextBox;
private JButton searchButton;
private JTextField NHSRegistrationNoTextField;
private JTextField patientNameTextField;
private JTextField addressTextField;
private JTextField postCodeTextField;
private JTextField conditionTextField;
private JButton confirmAndSendButton;

public MainOffice() {
    add(MainPanel);
    setSize(400, 400);

    try {
        Registry patientRegistry = LocateRegistry.getRegistry(1096);
        Registry registry = LocateRegistry.getRegistry(1099);

        testRecord = (IPatientRecord) patientRegistry.lookup("patientRecord");
        appLayer = (IAppLayer) registry.lookup("appLayer");

        System.out.println("Patient Record Interface Ready");

    }
    catch (Exception ex) {
        System.out.println("Exception Creating Patient Record Interface: " + ex.getMessage());
    }


    searchButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {

                testRecord = appLayer.getPatientRecord(Integer.parseInt(searchTextBox.getText()));
                System.out.println("Test Record " + testRecord.getName());

                updateForm();

                System.out.println("Patient returned to Main Office " + testRecord.getName());

            }
            catch (Exception ex) {
                System.out.println("Exception in Search Button: " + ex.getMessage());

            }
        }
    });

    confirmAndSendButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {

                System.out.println("Patient: " + testRecord.getName());

                System.out.println("Confirm and send pressed/nDetails sent: " + testRecord.getName());
                appLayer.sendToAmbulance(testRecord);


            }
            catch (Exception ex) {
                System.out.println("confirm and Send Exception: " + ex.getMessage());
            }



        }
    });
}

public void updateForm() {

    try{

        NHSRegistrationNoTextField.setText(String.valueOf(testRecord.getNhsRegistrationNo()));
        patientNameTextField.setText(testRecord.getName());
        addressTextField.setText(testRecord.getAddress());
        postCodeTextField.setText(testRecord.getPostCode());
        conditionTextField.setText(testRecord.getCondition());
    }
    catch (RemoteException rex) {
        System.out.println("Update form exception: " + rex.getMessage());
    }
}


public static void main (String args[] ) {
    MainOffice mainOffice = new MainOffice();
    mainOffice.setVisible(true);
}

My App LAyer:

public class AppLayer implements IAppLayer {

List ambulances;
List hospitals;

Dictionary ambulanceDictionary;

IPatientRecord patientRecord;

public AppLayer() throws RemoteException{
    ambulances = new ArrayList();
    hospitals = new ArrayList();

    ambulanceDictionary = new Hashtable();

}

@Override
public IPatientRecord getPatientRecord(int nhsNo) throws RemoteException {
    try {
        Registry registry = LocateRegistry.getRegistry(null,1098);

        IDataLayer dataLayer = (IDataLayer) registry.lookup("dataLayer");

        patientRecord = dataLayer.getPatientRecord(nhsNo);

        System.out.println("Patient returned: " + patientRecord.getName());

        //sendToAmbulance(patientRecord);

        return patientRecord;

    }
    catch (Exception ex) {
        System.out.println("Error in AppLayer: " + ex.getMessage() + " /n/nSee getPatientRecord in AppLayer");
        return null;
    }
}

The Function i Used to send to the other UI: The UI registers with the applayer first using another function. This uses that information to send the record to the right client essentially. The Main office UI calls this on through the applayer interface

public void sendToAmbulance(IPatientRecord patientRecord) {

Enumeration keys = ambulanceDictionary.keys();
    Enumeration elements = ambulanceDictionary.elements();

    int port = Integer.parseInt(keys.nextElement().toString());
    String callSign = ambulanceDictionary.get(port).toString();

    try {

        System.out.println("Searching on port: " + port);
        Registry registry2 = LocateRegistry.getRegistry(port);

        System.out.println("Looking up callSign: " + callSign);
        System.out.println("made it here");
        IAmbulance ambulance = (IAmbulance) registry2.lookup(callSign);

        System.out.println("and here");
        ambulance.Update(patientRecord);

        System.out.println("maybe here");

        if (ambulance.Update(patientRecord)){
            ambulanceDictionary.remove(port);
            System.out.println(callSign + "removed from list");
            System.out.println("");
        }
        else{
            System.out.println("Failed to update ambulance " + callSign);
        }

    }
    catch (Exception ex) {

        System.out.println("Exception in sendToAmbulance: " + ex.getMessage());
    }
}

The above function the calls Update on the Ambulance

public Boolean Update(IPatientRecord patientRecord) {

    try {
        NHSRegistrationNoTextField.setText(String.valueOf(patientRecord.getNhsRegistrationNo()));
        patientNameTextField.setText(patientRecord.getName());
        addressTextField.setText(patientRecord.getAddress());
        postCodeTextField.setText(patientRecord.getPostCode());
        conditionTextField.setText(patientRecord.getCondition());

        ReadyStatusTxt.setText("ON CALL");

        return true;
    }
    catch (Exception ex) {
        System.out.println("Exception in Update: " + ex.getMessage());
        return false;
    }
}

It doesn't even update the ReadyStatusTxt.setText("On Call") yet returns true!

I've tried so many things. Originally i wasnt using the Patient record interface between the layers, I was being lazy but I've updated it so each different layer has to go through the Patient Interface and it still works the same way as before.

I added a seperate function to ambulance that would then call the database again pull the details for itself and then update it with this and it still will not update.

Is It as simple as I cant drive the update from an external class

Oh and another thing is if i put System.out.Println(patientRecord.getName()); in the above function it prints out the ******* name that i want to update the above aforementioned JtextField. I've tried saving that into Strings first and then adding it to the JtextField but that doesn't work either

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sfa20
  • 63
  • 1
  • 8
  • I guess your `update` method is not being called in Swing EDT. Call `update` using `SwingUtilities.invokeLater`. – Dakshinamurthy Karra Nov 22 '18 at 15:06
  • Not sure i did i right but i Made AmbulanceClass implement Runnable and changed the Update function to public Runnable Update() and it's simply trying to update one Jtextlabel ReadyStatusTxt.setText("ON CALL"); when this runs through SwingUtilities.invokeLater i get Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" – sfa20 Nov 22 '18 at 15:51
  • For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 22 '18 at 22:47

0 Answers0