1

I have a problem that I've stuck on for days, and I didn't find any answer any were. I'll try to describe it as best as I can.

I have an fxml file which displays a Text and a button, and I would like to make the text of the Text change as I press the button. The text I want it to display is the value attributed to a key in a map.

Fxml file:

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

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

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="768" prefWidth="1366" styleClass="root" stylesheets="/PagTest/style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="PagTest.SessionController">
   <children>
      <Text fx:id="text1" layoutX="750.0" layoutY="455.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Text" AnchorPane.bottomAnchor="309.0" AnchorPane.leftAnchor="750.0" AnchorPane.rightAnchor="593.86328125" AnchorPane.topAnchor="442.0" />
      <Button fx:id="button" onAction="#button" layoutX="649.0" layoutY="438.0" mnemonicParsing="false" text="Button" />
   </children>
</AnchorPane>

the associated controller

package PagTest;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;
import javafx.scene.control.*;

import java.io.*;
import java.net.URL;
import java.util.*;


public class SessionController implements Initializable, ControlledScreen{

    public Text text1;
    public Button button;
    private String mFileName;
    ScreensController myController;
    private static Map <String, String> mPatient = new HashMap<>();

    public void setScreenParent (ScreensController screenParent) {myController = screenParent;}

    @FXML
    private void goToWelcomeScreen (ActionEvent event) {myController.setScreen(ScreensFramework.screen1ID);}

    @Override
    public void initialize(URL location, ResourceBundle resources) {}
    public void exit (ActionEvent event) {Platform.exit();}

    public void setSession(String fileName) throws IOException {
        mFileName = fileName;
        BufferedReader br = new BufferedReader(new FileReader(mFileName));
        String line = "";
        while ((line = br.readLine()) != null){
            String parts[] = line.split("\t");
            mPatient.put(parts[0], parts[1]);
        }
        br.close();
        System.out.println(mPatient.toString());
        System.out.println(mPatient.get("firstName"));
    }

    public void button(ActionEvent event) {
        System.out.println(mPatient.toString());
        text1.setText(mPatient.get("firstName"));
    }
}

The two System.out.println at the end of the setSession method work perfectly

If a ask the code to replace the text by "Hello" it works, but when I ask it to replace it by the value associated to the firstName key in the map it doesn't work and I get an error.

Ultimately I would like that the text of the Text object is changed as the scene is displayed without having to press any button.

I hope it is sufficiently clear, I'm very new to java.

Thanks for the help.

EDIT: When I reopend Intellij IDEA to paste the error, I tried again, and the text is correctly changed when I click the button.... I don't know why. But when I put the text1.setText(mPatient.get("firstName"); in the setSession method, I get the following error.

The new code:

package PagTest;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;
import javafx.scene.control.*;

import javax.swing.text.JTextComponent;
import java.io.*;
import java.net.URL;
import java.util.*;


public class SessionController implements Initializable, ControlledScreen{

    public Text text1;
    public Button button;
    private String mFileName;
    ScreensController myController;
    private static Map <String, String> mPatient = new HashMap<>();

    public void setScreenParent (ScreensController screenParent) {myController = screenParent;}

    @FXML
    private void goToWelcomeScreen (ActionEvent event) {myController.setScreen(ScreensFramework.screen1ID);}

    @Override
    public void initialize(URL location, ResourceBundle resources) {}
    public void exit (ActionEvent event) {Platform.exit();}

    public void setSession(String fileName) throws IOException {
        mFileName = fileName;
        BufferedReader br = new BufferedReader(new FileReader(mFileName));
        String line = "";
        while ((line = br.readLine()) != null){
            String parts[] = line.split("\t");
            mPatient.put(parts[0], parts[1]);
        }
        br.close();
        System.out.println(mPatient.toString());
        System.out.println(mPatient.get("firstName"));
        text1.setText(mPatient.get("firstName"));
    }

    public void button(ActionEvent event) {
        System.out.println(mPatient.toString());
    }
}

The setSession() method is called from another class called WelcomeController which passes the fileName and sets the scene of Session.fxml

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.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:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    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.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: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$355(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$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
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:497)
    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:497)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 52 more
Caused by: java.lang.NullPointerException
    at PagTest.SessionController.setSession(SessionController.java:44)
    at PagTest.WelcomeController.goToPatientList(WelcomeController.java:30)
    ... 62 more

EDIT2: Here is the WelcomeController from where the setSession(...) is called in the goToSession when a button is clicked

package PagTest;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class WelcomeController implements Initializable, ControlledScreen {


    ScreensController myController;

    public void setScreenParent (ScreensController screenParent) {
        myController = screenParent;
    }

    @FXML
    private void goToWelcomeScreen (ActionEvent event) {myController.setScreen(ScreensFramework.screen1ID);}

    @FXML
    private void goToNewPatientForm (ActionEvent event) {myController.setScreen(ScreensFramework.screen2ID);}

    @FXML
    private void goToSession (ActionEvent event) throws IOException {
        SessionController session = new SessionController();
        session.setSession("resources\\PatientForms\\Pierre-Alexandre Gross.txt");
        myController.setScreen(ScreensFramework.screen4ID);
    }

    @FXML
    private void exit (ActionEvent event) {
        Platform.exit();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }
}
  • Post the stack trace. Where are you calling `setSession(...)` from? – James_D Nov 01 '15 at 23:10
  • **it doesn't work and I get an error.** Can u post the error??? – Madushan Perera Nov 01 '15 at 23:23
  • I assume line 44 is `text1.setText(mPatient.get("firstName"));`, correct? Are you calling `setSession(...)` on the same **object** that is created when the FXML is loaded? – James_D Nov 01 '15 at 23:38
  • Yes line 44 is correct, I've added the class file from where the setSession(...) method is called. – GROSS Pierre-Alexandre Nov 01 '15 at 23:46
  • You are creating a new object and calling `setSession(...)`. `text1` is associated with the FXML file (via the attribute `fx:id="text1"`). This means when you load the FXML file, the `FXMLLoader` creates an instance of the controller class (`SessionController`) and initializes `text1` on that instance. But you are calling `setSession` on a completely different instance (one you create yourself with `new SessionController()`) and so `text1` is not initialized. Hence you get a null pointer exception when you try to reference it. – James_D Nov 01 '15 at 23:49
  • OK... I think I understand what you are saying. But I have to creat a new SessionController to be able to call the setSession method from the WelcomController class right? or is there another way? – GROSS Pierre-Alexandre Nov 02 '15 at 00:07
  • You can get the `SessionController` from the `FXMLLoader` after you load the FXML file, and pass it to the `WelcomeController` (or something similar). See if http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310 helps – James_D Nov 02 '15 at 00:33

0 Answers0