0

I have a problem switching from 1 fxml file into another (created in Scene Builder 2.0) and everything I find on internet says the same, but the code just doesnt work for me, any help will be highly appreciated:

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * u/author andys
 */
public class ScenestartController implements Initializable {
    u/FXML
    private AnchorPane paneone;
    u/FXML
    Button manualbutton;
    u/FXML
    Button simbutt;
    /**
     * Initializes the controller class.
     */
    u/Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        System.out.println("Loaded");

    }    

    u/FXML
    private void handlemanualbutton(ActionEvent event) throws Exception {
        Parent rooter = FXMLLoader.load(getClass().getResource("/fxml/drawbridgefirst.fxml"));
        Scene sceno = new Scene(rooter);
        Stage stago = (Stage)((Node)event.getSource()).getScene().getWindow();
        stago.setTitle("Manual");
        stago.setScene(sceno);
        stago.show();
    }

    u/FXML
     private void handleSimButt(ActionEvent event) throws Exception {
        Parent rooters = FXMLLoader.load(getClass().getResource("/fxml/drawbridgefirst.fxml"));
        Scene scenos = new Scene(rooters);
        Stage stagos = (Stage)((Node)event.getSource()).getScene().getWindow();
        stagos.setTitle("simulation");
        stagos.setScene(scenos);
        stagos.show();
}}

This is a start scene that loads from main, but when I press the manualbutton or simbutt button (to switch to one of the other fxml files) I get this error:

WARNING: Loading FXML document with JavaFX API of version 11.0.1 by JavaFX `runtime of version 8.0.231 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.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.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:394) 
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$2(GlassViewEventHandler.java:432) 
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431) 
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$4(WinApplication.java:186) 
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)    ... 48 more Caused by: javafx.fxml.LoadException:  /C:/Users/andys/Documents/Fontys/Semester3/prj3-repair/repair-rudystylz/Project3Repf/target/classes/fxml/drawbridgefirst.fxml  
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
at ScenestartController.handlemanualbutton(ScenestartController.java:60)    ... 58 more Caused by: java.lang.NullPointerException 
at DrawbridgefirstController.initialize(DrawbridgefirstController.java:32) 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)    ... 66 more

The DrawbridgefirstController:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


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

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

/**
 * FXML Controller class
 *
 * @author andys
 */
public class DrawbridgefirstController implements Initializable {
    @FXML
    public TextField texta;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        texta.setText("Boat approached and pressed the button");
    }

    @FXML
    public void upbuttclick(ActionEvent actionEvent) {
        texta.setText("Bridge is going up!");


    }

    @FXML
    public void downbuttclick(ActionEvent actionEvent) {
        texta.setText("Bridge is going down!");
    }

    @FXML
    public void maintbuttclick(ActionEvent actionEvent) {
        texta.setText("Bridge is in maintenance!");
    }
}

drawbridgefirst.fxml code:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="panetu" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="DrawbridgefirstController">
   <children>
      <Button id="upbutton" layoutX="509.0" layoutY="51.0" mnemonicParsing="false" onAction="#upbuttclick" text="Up" />
      <Button id="downbutton" layoutX="501.0" layoutY="107.0" mnemonicParsing="false" onAction="#downbuttclick" text="Down" />
      <Button id="maintenance" layoutX="482.0" layoutY="159.0" mnemonicParsing="false" onAction="#maintbuttclick" text="Maintenance" />
      <TextField id="texta" layoutX="46.0" layoutY="261.0" prefHeight="104.0" prefWidth="407.0" />
   </children>
</AnchorPane>
  • Can you format the stack trace so we can see it... Just paste it as-is (with line breaks) and format it as code. – James_D Apr 17 '20 at 22:40
  • 1
    @James_D, I edited the stacktrace. – SedJ601 Apr 17 '20 at 22:42
  • 1
    It says there’s a null pointer exception in a controller whose code you haven’t posted. – James_D Apr 17 '20 at 22:45
  • @James_D I added the controller code – Ondra Hrubý Apr 18 '20 at 00:02
  • So clearly `texta` is null. – James_D Apr 18 '20 at 00:09
  • @James_D So the fxml file doesnt pass the TextField (created in SceneBuilder) to the controller? (I added the code for the fxml file) – Ondra Hrubý Apr 18 '20 at 12:17
  • shouldn't that be fx:id to inject fields? – kleopatra Apr 18 '20 at 13:39
  • @OndraHrubý Not if you don't define an `fx:id`. – James_D Apr 18 '20 at 13:41
  • @James_D Oh I see what you mean, but when I set the fx:id in the fxml file to texta it says ,,no injectable field found in fxml controller class for the id 'texta'" although in the Drawbridgefirst Controller the id of the TextField is indeed texta – Ondra Hrubý Apr 18 '20 at 15:07
  • Where does it say that? FXML files are not compiled, so this just sounds like a spurious IDE message. Does it run when you set the `fx:id` (or, at least, stop generating that exact error)? – James_D Apr 18 '20 at 15:17
  • @James_D It displays that message in Scene Builder when I set the fx:id, I saved it anyway, but the error remains when I try to switch the scenes – Ondra Hrubý Apr 18 '20 at 15:39
  • Is it the exact same error? Maybe clean and rebuild the project... – James_D Apr 18 '20 at 15:46
  • @James_D Ok, I found the problem, I had one more slash before the fxml (`getResource("/fxml/drawbridgefirst.fxml")`), such a small detail :D But thank you all very much for help, really appreciate it:) – Ondra Hrubý Apr 18 '20 at 20:55

0 Answers0