2

I am trying to get my JavaFX program to swtich between screens when a button is pressed, but I am experiencing difficulty with this. I am getting a long string of error messages one of which is 'java.lang.NullPointerException: Location is required.' All of my files for the program are held within the same package.

Main Method:

package therealcompsciia;  

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TheRealCompSciIA extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("NewSoccerAppInitialScreen.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

Here is the code for the initial screen FXML, NewSoccerAppInitialScreen.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="336.0" prefWidth="456.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="NewSoccerAppInitialScreenController"> 
  <children>
  <Button fx:id="PlayerProfiles" alignment="CENTER" layoutX="15.0" layoutY="148.0" mnemonicParsing="false" onAction="#playerprofilesButton" prefHeight="90.0" prefWidth="134.0" text="Player Profiles" textFill="#bf1616">
     <font>
        <Font size="14.0" />
     </font></Button>
  <Button fx:id="Statistics" alignment="CENTER" layoutX="162.0" layoutY="148.0" mnemonicParsing="false" onAction="#statisticsButton" prefHeight="90.0" prefWidth="134.0" text="Statistics" textFill="#bf1616">
     <font>
        <Font size="14.0" />
     </font>
  </Button>
  <Button fx:id="FormationEditor" alignment="CENTER" layoutX="308.0" layoutY="148.0" mnemonicParsing="false" onAction="#formationeditorButton" prefHeight="90.0" prefWidth="134.0" text="Formation Editor" textFill="#bf1616">
     <font>
        <Font size="14.0" />
     </font>
  </Button>
  <ImageView fitHeight="98.0" fitWidth="198.0" layoutY="238.0" pickOnBounds="true" preserveRatio="true">
     <image>
        <Image url="@CompSci%20IA%20Grass.jpe" />
     </image>
  </ImageView>
  <ImageView fitHeight="98.0" fitWidth="198.0" layoutX="135.0" layoutY="238.0" pickOnBounds="true" preserveRatio="true">
     <image>
        <Image url="@CompSci%20IA%20Grass.jpe" />
     </image>
  </ImageView>
  <ImageView fitHeight="98.0" fitWidth="198.0" layoutX="255.0" layoutY="238.0" pickOnBounds="true" preserveRatio="true">
     <image>
        <Image url="@CompSci%20IA%20Grass.jpe" />
     </image>
  </ImageView>
  <ImageView fitHeight="98.0" fitWidth="198.0" layoutX="316.0" layoutY="238.0" pickOnBounds="true" preserveRatio="true">
     <image>
        <Image url="@CompSci%20IA%20Grass.jpe" />
     </image>
  </ImageView>
  <ImageView fitHeight="78.0" fitWidth="107.0" layoutX="296.0" layoutY="76.0" pickOnBounds="true" preserveRatio="true">
     <image>
        <Image url="@CompSci%20IA%20Goal.jpe" />
     </image>
  </ImageView>
  <ImageView fitHeight="98.0" fitWidth="64.0" layoutX="63.0" layoutY="73.0" pickOnBounds="true" preserveRatio="true">
     <image>
        <Image url="@CompSci%20IA%20Soccer%20Ball.jpe" />
     </image>
  </ImageView>
  <Label layoutX="114.0" layoutY="30.0" text="DM Soccer Manager" textFill="#bf1616">
     <font>
        <Font name="System Bold" size="24.0" />
     </font>
  </Label>
  </children>
  </AnchorPane>

Here is the code for my initial screen controller, NewSoccerAppInitialScreenController.java

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
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.stage.Stage;

public class NewSoccerAppInitialScreenController implements Initializable {

@FXML
private Button PlayerProfiles;
@FXML
private Button Statistics;
@FXML
private Button FormationEditor;

    @FXML
    private void formationeditorButton(ActionEvent event) throws IOException{
    Parent FXMLDocument2Parent = FXMLLoader.load(getClass().getClassLoader().getResource("fxml_NewFormationsInitialScreen.fxml"));
    Scene FXMLDocument2Scene = new Scene(FXMLDocument2Parent);
    Stage AppStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    //AppStage.hide();
    AppStage.setScene(FXMLDocument2Scene);
    AppStage.show();
    }

    @FXML
    private void playerprofilesButton(ActionEvent event) throws IOException{
    Parent FXMLDocument2Parent = FXMLLoader.load(getClass().getClassLoader().getResource("fxml_NewPlayerProfilesInitial.fxml"));
    Scene FXMLDocument2Scene = new Scene(FXMLDocument2Parent);
    Stage AppStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    //AppStage.hide();
    AppStage.setScene(FXMLDocument2Scene);
    AppStage.show();
    }

    @FXML
    private void statisticsButton(ActionEvent event) throws IOException{
    Parent FXMLDocument2Parent = FXMLLoader.load(getClass().getClassLoader().getResource("fxml_NewStatisticsInitial.fxml"));
    Scene FXMLDocument2Scene = new Scene(FXMLDocument2Parent);
    Stage AppStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    //AppStage.hide();
    AppStage.setScene(FXMLDocument2Scene);
    AppStage.show();
    }


@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}

Here is the error message:

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:8413)
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: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$354(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$148(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: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: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
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 NewSoccerAppInitialScreenController.formationeditorButton(NewSoccerAppInitialScreenController.java:37)
... 58 more

I am not sure what the problem is. The file names are correct and all exist in the same package under the same project. My initial screen loads and is visible, but as soon as I click one of the buttons to send me to a new screen, the error message above is displayed.

  • The problem is that `getClass().getClassLoader().getResource("fxml_NewPlayerProfilesInitial.fxml")` returns null as it can't find the resource. Maybe [this](http://stackoverflow.com/a/28266774/5115768) might help you? – lenny Mar 01 '17 at 10:52

1 Answers1

0

Your issue seems to be the same as those 2:

Community
  • 1
  • 1
Boschi
  • 622
  • 3
  • 10