0

I'm getting errors while transfer data from one form to another. Process is like, when i click edit button on 1st form 2nd form will open there is list of customers to choose from for edit option. Once i clicked any customer 2nd form will close and data of that customer nee to show for edit in textfield of 1st form. Problem is when i do this, I'm getting error which i can't understand.

@FXML
javafx.scene.control.TextField companyName_Create_Client;

Edit Clicked event on 1st controller :

@FXML
protected void edit_Create_ClientClicked() {
    try {
        edit_Create_Client.setDisable(true);
        editClient = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("editClient.fxml"));
        Scene scene = new Scene(root);
        editClient.setScene(scene);
        editClient.setTitle("Select Client");
        editClient.initModality(Modality.APPLICATION_MODAL);
        editClient.initStyle(StageStyle.UTILITY);
        editClient.show();

    } catch (Exception e) {
        System.out.println("Error :" + e.getMessage());
    }
}

Event when customer selected in 2nd controller:

@FXML
protected void find_table_editClientClicked(){
    Stage previousStage=(Stage) find_editClient.getScene().getWindow();
    client client=(client) find_table_editClient.getSelectionModel().selectedItemProperty().getValue();
    mainController.fillDataToEdit(client.getCompanyName());
    previousStage.close();
}

Function on 1st controller to fill data:

public void fillDataToEdit(String selectedCompany) {
    try {        
        //System.out.println("company :" + selectedCompany);
        Session session = new Configuration().configure().buildSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        Iterator s = session.createQuery("from client where companyName='" + selectedCompany + "'").iterate();
        client client = (client) s.next();
        System.out.println("company2 :" + client.getCompanyName());
        String com=client.getCompanyName();
        companyName_Create_Client.setText(com);

        transaction.commit();
        session.close();
    } catch (Exception e) {
        System.out.println("Error :" + e.getMessage());
        e.printStackTrace();
    }
}

Error is comming from companyName_Create_Client.setText(com); line.

Error :

company2 :Demo Company  // see, data from 2nd form is comming still error while assigning to textfield of 1st form
Error :null
java.lang.NullPointerException
at licensemonitoringsystem.mainController.fillDataToEdit(mainController.java:209)
at licensemonitoringsystem.EditClientController.find_table_editClientClicked(EditClientController.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
hotzst
  • 7,238
  • 9
  • 41
  • 64
  • How are you getting the reference to the first controller into the `mainController` field in the second controller? There is nowhere I can see where you tie the controller instances together. – James_D Mar 25 '16 at 12:17
  • Shouldn't `String com=client.getCompanyName();` be after you commit the transaction? The client would be null if you hadn't actually ran the transaction, right? (never did it this way, so just a speculation) – Robert Martin Mar 25 '16 at 12:22
  • Where in your `mainController.java` is line 209? – hotzst Mar 25 '16 at 13:57
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – hotzst Mar 25 '16 at 13:58
  • @RobertMartin I already check that possibility and confirmed that is not problem. Even I directly put string in `companyName_Create_Client.setText("Demo");` then too got same problem. Probably that is because focus is still not on mainController. and I think this is focus problem just cause when i close application `jfxsa-run` process still running which is may be tread from second form. – Prashant Rahate Mar 26 '16 at 06:28
  • @hotzst line 209 is `companyName_Create_Client.setText(com);`. And dear this is not problem of null pointer exception. because got same even after passing direct string. – Prashant Rahate Mar 26 '16 at 06:33
  • `companyName_Create_Client` is not yet initialized when you try to set the text. hence the `NullPointerException`. – hotzst Mar 26 '16 at 07:00
  • @hotzst please help me with how do i initialize `companyName_Create_Client ` which is actually component of form that still running. And also what is to initialize ? – Prashant Rahate Mar 26 '16 at 07:06

0 Answers0