Search code examples
javajavafxfxml

How do I get selected text in textArea in main controller class from second controller class?


I am trying to get the selected text from a textArea in my FXMLDocumentController.java and pass it to a second controller class, HeadingsController.java.

I am running the app in Netbeans and it starts up and loads fine. I get the nullPointerException when I click the Hyperlink with fx:id="h1Link". the null pointerException says its at line 27 of HeadingsController.java which is

String selectedText = fxmlDocC.getSelectedTextfromTextArea();

FXMLDocumentController.java is where the textArea im trying to reference is...

package textareatest1;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;


public class FXMLDocumentController {

    @FXML public TextArea textArea;
    String selectedText = new String();
    @FXML private HeadingsController headingsController = new HeadingsController();

    public String getSelectedTextfromTextArea(){
        selectedText =  textArea.getSelectedText();
        return selectedText;
    }

    public void replaceSelectedText(String string){
        textArea.replaceSelection(string);
    }

    @FXML public void initialize() {
        headingsController.init(this);
        textArea.setWrapText(true);

    }   

}

AND here is the FXML file for the above controller...

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane fx:id="borderPaneRoot" prefHeight="600.0" prefWidth="1000.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="textareatest1.FXMLDocumentController" >
    <top>

    </top>
    <center>
        <TextArea fx:id="textArea" />
    </center>
    <left>
        <GridPane id="gridpaneLeft" fx:id="gridpaneLeft" hgap="10.0" prefHeight="200.0" prefWidth="320.0" vgap="10.0" >
            <Accordion fx:id="leftAccordion" prefHeight="250.0" prefWidth="680.0">
                <panes>

                    <TitledPane fx:id="headingsTitlePane" animated="true" text="Headings">
                        <content>

                                <Pane>
                                    <children>
                                        <fx:include fx:id="fxmlheadings" source="FXMLHeadings.fxml"/>
                                    </children>
                                </Pane>
                        </content>
                    </TitledPane>

                </panes>
            </Accordion>
        </GridPane>
    </left>

</BorderPane>

Here is my second controller, HeadingsControllerr.java .....

package textareatest1;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Hyperlink;


public class HeadingsController {
    @FXML private FXMLDocumentController fxmlDocC;


    String[] headings = {"h1. ","h2. ","h3. ","h4. ","h5. ","h6. "};
    @FXML public Hyperlink h1Link;

    @FXML
    private void handleH1LinkAction(ActionEvent event) {

        String selectedText = fxmlDocC.getSelectedTextfromTextArea();
        System.out.println(selectedText);
//        System.out.println("test");
    }

    public void init(FXMLDocumentController fxmlDocumentController) {
           fxmlDocC = fxmlDocumentController;
    }
}

AND here is the FXML file for the HeadingsController.java...

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="textareatest1.HeadingsController" fx:id="fxmlheadings">

    <Hyperlink id="links" fx:id="h1Link" text="h1. Biggest heading" layoutX="5.0" layoutY="3.0" onAction="#handleH1LinkAction"  />


</AnchorPane>

Here is the stacktrace with the nullPointerException I am getting which I believe is because fxmlDocC is null... I think I need to create an instance of FXMLDocumentController properly?

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    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.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:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Hyperlink.fire(Hyperlink.java:153)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    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.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$MouseHandler.process(Scene.java:3757)
    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:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    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$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    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:1769)
    ... 56 more
Caused by: java.lang.NullPointerException
    at textareatest1.HeadingsController.handleH1LinkAction(HeadingsController.java:27)
    ... 66 more

I am trying to highlight text in the TextArea through the UI, and have it print in the console when I click the hyperlink with fx:id="h1Link".


Solution

  • This issue is not solved by creating an instance of FXMLDocumentController, but by making sure the controller you pass the instance to is the one used with the included fxml; the one you create with new HeadingsController() is not this instance.

    Instead the fact that FXMLLoader injects the controller for the included element to a field with the name you get, if you append Controller to the fx:id of the <fx:include> element:

    public class FXMLDocumentController {
    
        @FXML public TextArea textArea;
        String selectedText = ""; // why is this field necessary???
    
        // controller for FXMLHeadings.fxml is automatically injected here
        @FXML private HeadingsController fxmlheadingsController;
    
        public String getSelectedTextfromTextArea(){
            selectedText =  textArea.getSelectedText();
            return selectedText;
        }
    
        public void replaceSelectedText(String string){
            textArea.replaceSelection(string);
        }
    
        @FXML public void initialize() {
            fxmlheadingsController.init(this);
            textArea.setWrapText(true);
        }   
    
    }