Java Programming (JavaFX)

Hello all,

I am creating a java program using javafx and am a little stumped. When a button is pressed another scene is supposed to open up but nothing is happening.

Here is a preview of my code:

Stage prevStage;
public void setPrevStage(Stage stage)
{
    this.prevStage = stage;
}




@FXML
private void btn1Action(ActionEvent event) throws Exception
{
  
        Stage stage = new Stage();
        Pane myPane = null;
        myPane = FXMLLoader.load(getClass().getResource("/com/Booking.fxml"));
        Scene scene = new Scene(myPane);
        stage.setScene(scene);
        prevStage.close();
        stage.show();
}

My error output log:

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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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)
… 45 more
Caused by: java.lang.NullPointerException
at com.MenuController.btn1Action(MenuController.java:41)
… 55 more

Any help is greatly appreciated. Thanks in advance!

-bd

Are you getting the exception at runtime or compile time? Without seeing all of your code it’s difficult to diagnose the problem here. But I do see that you have an NPE in the method that you have displayed on line 41. Is that the line where you load your FXML file?

The exception comes when the program is running.

        Stage stage = new Stage();

Line 41–> Pane myPane = null;
myPane = FXMLLoader.load(getClass().getResource("/com/Booking.fxml"));
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
stage.show();

I am fairly new to JavaFX :frowning:

I haven’t used JavaFX myself in several months. Are you certain about the path to the FXML file? Is your FXML located in src/main/java or src/main/resources? I feel like the NullPointerException is being caused by not being able to find your FXML file.

My project package name is “com” so the file path should look through my package for the file named “Booking.fxml”.

The beginning of my program is a login screen and the fxml code for that file is the exact same code and it logs a user in perfectly. That fxml file path is exactly the same so I figured it should work in the new fxml file. I will try to see if it will work once I change the file path.

I don’t know if this will help but I am attaching my fxml and two other classes:

MenuController:

package com;


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.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 */
public class MenuController implements Initializable 
{

Stage prevStage;
public void setPrevStage(Stage stage)
{
    this.prevStage = stage;
}



@FXML
private void btn1Action(ActionEvent event) throws IOException
{
    
    
    
  
        Stage stage = new Stage();
        Pane myPane = null;
        myPane = FXMLLoader.load(getClass().getResource("/com/Booking.fxml"));
        Scene scene = new Scene(myPane);
        stage.setScene(scene);
        prevStage.close();
        stage.show();
}



@FXML
private void btn2Action(ActionEvent event) throws Exception
{
  
        Stage stage = new Stage();
        Pane myPane = null;
        myPane = FXMLLoader.load(getClass().getResource("/com/Listing.fxml"));
        Scene scene = new Scene(myPane);
        stage.setScene(scene);
        prevStage.close();
        stage.show();
}

@FXML
private void btn3Action(ActionEvent event) throws Exception
{
  
        Stage stage = new Stage();
        Pane myPane = null;
        myPane = FXMLLoader.load(getClass().getResource("/com/Status.fxml"));
        Scene scene = new Scene(myPane);
        stage.setScene(scene);
        prevStage.close();
        stage.show();
}

@FXML
private void btn4Action(ActionEvent event) throws Exception
{
  
        Stage stage = new Stage();
        Pane myPane = null;
        myPane = FXMLLoader.load(getClass().getResource("/com/Release.fxml"));
        Scene scene = new Scene(myPane);
        stage.setScene(scene);
        prevStage.close();
        stage.show();
}

@FXML
private void btn5Action(ActionEvent event) throws Exception
{
  
        Stage stage = new Stage();
        Pane myPane = null;
        myPane = FXMLLoader.load(getClass().getResource("/com/CourtDates.fxml"));
        Scene scene = new Scene(myPane);
        stage.setScene(scene);
        prevStage.close();
        stage.show();
}

@FXML
private void btn6Action(ActionEvent event) throws Exception
{
  
        Stage stage = new Stage();
        Pane myPane = null;
        myPane = FXMLLoader.load(getClass().getResource("/com/UpdateStatus.fxml"));
        Scene scene = new Scene(myPane);
        stage.setScene(scene);
        prevStage.close();
        stage.show();
}

@FXML
private void btn7Action(ActionEvent event) throws Exception
{
  
        Stage stage = new Stage();
        Pane myPane = null;
        myPane = FXMLLoader.load(getClass().getResource("/com/AllInmates.fxml"));
        Scene scene = new Scene(myPane);
        stage.setScene(scene);
        prevStage.close();
        stage.show();
}


@FXML
private void btn8Action(ActionEvent event) throws Exception
{
  
        Stage stage = new Stage();
        Pane myPane = null;
        myPane = FXMLLoader.load(getClass().getResource("/com/Visitors.fxml"));
        Scene scene = new Scene(myPane);
        stage.setScene(scene);
        prevStage.close();
        stage.show();
}


@FXML
private void btn9Action(ActionEvent event) throws Exception
{
  
   
        Stage stage = new Stage();
        Pane myPane = null;
        myPane = FXMLLoader.load(getClass().getResource("/com/Exit.fxml"));
        Scene scene = new Scene(myPane);
        stage.setScene(scene);
        prevStage.close();
        stage.show();
}


/**
 * Initializes the controller class.
 * @param url
 * @param rb
 */
@Override
public void initialize(URL url, ResourceBundle rb) 
{
}    

}

Menu.java

package com;

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

public class Menu extends Application 
{

@Override
public void start(Stage stage) throws Exception 
{
   FXMLLoader myLoader = new FXMLLoader(getClass().getResource("/com/Menu.fxml"));
   Pane myPane = (Pane)myLoader.load();
   MenuController controller1 = (MenuController) myLoader.getController();
   controller1.setPrevStage(stage);
   myPane.setStyle("-fx-background-color: #2648ae");
   Scene myScene = new Scene(myPane);
   stage.setTitle("Sheriff's Office");
   stage.setScene(myScene);
   stage.sizeToScene();
   stage.show();
}

}

Menu.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.MenuController">
   <children>
  <Button fx:id="btn1" layoutX="102.0" layoutY="101.0" mnemonicParsing="false" onAction="#btn1Action"
text="Book a new inmate" textFill="#57bf23" />
  <Text fill="WHITE" fontSmoothingType="LCD" layoutX="115.0" layoutY="39.0" strokeType="OUTSIDE"
strokeWidth="0.0" text="Sheriff's Office">
     <font>
        <Font size="26.0" />
     </font>
  </Text>
  <Button fx:id="btn4" layoutX="385.0" layoutY="160.0" mnemonicParsing="false" onAction="#btn4Action" text="Release an inmate" textFill="#57bf23" />
  <Button fx:id="btn3" layoutX="78.0" layoutY="160.0" mnemonicParsing="false" onAction="#btn3Action" text="View inmate current status" textFill="#57bf23" />
  <Button fx:id="btn5" layoutX="57.0" layoutY="218.0" mnemonicParsing="false" onAction="#btn5Action" text="View court dates for all inmates" textFill="#57bf23" />
  <Button fx:id="btn6" layoutX="375.0" layoutY="218.0" mnemonicParsing="false" onAction="#btn6Action" text="Update inmate status" textFill="#57bf23" />
  <Button fx:id="btn7" layoutX="23.0" layoutY="272.0" mnemonicParsing="false" onAction="#btn7Action" text=" List all inmates (by name and cell block)" textFill="#57bf23" />
  <Button fx:id="btn2" layoutX="394.0" layoutY="101.0" mnemonicParsing="false" onAction="#btn2Action" text="List all inmates" textFill="#57bf23" />
  <Button fx:id="btn8" layoutX="319.0" layoutY="272.0" mnemonicParsing="false" onAction="#btn8Action" text="View visitors by name, date, and inmate" textFill="#57bf23" />
  <Button fx:id="btn9" layoutX="525.0" layoutY="339.0" mnemonicParsing="false" onAction="#btn9Action"
text="Exit" textFill="#2648ae" />
   </children>
</AnchorPane>

I remember having a similar issue with a JavaFX project that I did before. In that case, oddly enough, when I debugged the program it worked, but when I just ran it, it didn’t. I’m currently reading back through the source code to see how I solved it.

Okay thank you so much for your help!

My method for loading my FXML files is pretty similar to yours, except that I had a single class for handling views, and I used relative paths for my FXML files. I’m pretty sure that the best practice for loading FXML is that all of your files be put in a ResourceBundle on your classpath, but I’ve never went about it that way (as I said, I haven’t used JavaFX for months. I used it for one semester of college and never again). This tutorial is what I used to get up to speed on JavaFX and it may be helpful. If you want to check my horribly designed project, feel free, but I’m not sure what use that will be to you. This StackOverflow answer may also be of some use.

I’m sorry I couldn’t be of more help to you.

Yeah I’m in a Java class now where we’re learning about scenes and all that good stuff :expressionless:

Okay I will take a look into what you’ve sent me. Once again, thank you for all of your help!

Do you want multiple scene on same stage ?
or you want new stage with new scene on the click event ?

the issue is in your file path
use
“com/Booking.fxml”
instead of
“/com/Booking.fxml”