0

I'm trying to build a Library Management System, In the Home page I want when the user clicks a button it should popup that screen (for example - if I click add members button Add Members screen should popup and the user should be able to access it)

I have tried googling the error but to no avail

Error -

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1862)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1729)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8889)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3856)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1851)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2584)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
    ... 46 more
Caused by: java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:208)
    at Home.HomeController.loadWindow(HomeController.java:42)
    at Home.HomeController.loadAddBooks(HomeController.java:21)
    ... 57 more

Process finished with exit code 0

Home.fxml -

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

    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.ListView?>
    <?import javafx.scene.control.Menu?>
    <?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Tab?>
    <?import javafx.scene.control.TabPane?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.BorderPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.StackPane?>
    <?import javafx.scene.layout.VBox?>
    <?import javafx.scene.text.Font?>
    <?import javafx.scene.text.Text?>

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Home.HomeController">
   <children>
      <BorderPane prefHeight="200.0" prefWidth="200.0">
         <top>
            <MenuBar BorderPane.alignment="CENTER">
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Delete" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
         </top>
         <right>
            <VBox prefHeight="673.0" prefWidth="198.0" BorderPane.alignment="CENTER">
               <children>
                  <Button fx:id="AddMem" mnemonicParsing="false" onAction="#loadAddMembers" prefHeight="103.0" prefWidth="198.0" text="Add Member">
                     <VBox.margin>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="20.0" />
                     </VBox.margin>
                  </Button>
                  <Button fx:id="AddBook" layoutX="20.0" layoutY="20.0" mnemonicParsing="false" onAction="#loadAddBooks" prefHeight="103.0" prefWidth="198.0" text="Add Book">
                     <VBox.margin>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="20.0" />
                     </VBox.margin>
                  </Button>
                  <Button fx:id="ViewMem" layoutX="20.0" layoutY="143.0" mnemonicParsing="false" onAction="#loadViewMembers" prefHeight="103.0" prefWidth="198.0" text="View Members">
                     <VBox.margin>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="20.0" />
                     </VBox.margin>
                  </Button>
                  <Button fx:id="ViewBook" layoutX="20.0" layoutY="266.0" mnemonicParsing="false" onAction="#loadViewBooks" prefHeight="103.0" prefWidth="198.0" text="View Book ">
                     <VBox.margin>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="20.0" />
                     </VBox.margin>
                  </Button>
                  <Button layoutX="20.0" layoutY="389.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="198.0" text="Settings">
                     <VBox.margin>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="20.0" />
                     </VBox.margin>
                  </Button>
               </children>
            </VBox>
         </right>
         <center>
            <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
              <tabs>
                <Tab text="Issue">
                  <content>
                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                           <children>
                              <VBox prefHeight="642.0" prefWidth="802.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                                 <children>
                                    <HBox prefHeight="200.0" prefWidth="802.0">
                                       <children>
                                          <TextField alignment="CENTER" promptText="Enter Book ID">
                                             <HBox.margin>
                                                <Insets bottom="10.0" left="10.0" right="10.0" top="90.0" />
                                             </HBox.margin>
                                          </TextField>
                                          <VBox alignment="CENTER" prefHeight="200.0" prefWidth="634.0">
                                             <HBox.margin>
                                                <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                                             </HBox.margin>
                                             <children>
                                                <Text layoutX="10.0" layoutY="55.0" strokeType="OUTSIDE" strokeWidth="0.0" text="BOOK ID" wrappingWidth="178.884765625">
                                                   <VBox.margin>
                                                      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                                   </VBox.margin>
                                                </Text>
                                                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="BOOK NAME" wrappingWidth="178.08837890625">
                                                   <VBox.margin>
                                                      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                                   </VBox.margin>
                                                </Text>
                                                <Text layoutX="10.0" layoutY="23.0" strokeType="OUTSIDE" strokeWidth="0.0" text="BOOK AUTHOR" wrappingWidth="180.43017578125">
                                                   <VBox.margin>
                                                      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                                   </VBox.margin>
                                                </Text>
                                                <Text layoutX="10.0" layoutY="39.0" strokeType="OUTSIDE" strokeWidth="0.0" text="BOOK PUBLISHER" wrappingWidth="180.6640625">
                                                   <VBox.margin>
                                                      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                                   </VBox.margin>
                                                </Text>
                                             </children>
                                          </VBox>
                                       </children>
                                    </HBox>
                                    <HBox layoutX="10.0" layoutY="10.0" prefHeight="200.0" prefWidth="802.0">
                                       <children>
                                          <TextField alignment="CENTER" promptText="Enter Member ID">
                                             <HBox.margin>
                                                <Insets bottom="10.0" left="10.0" right="10.0" top="90.0" />
                                             </HBox.margin>
                                          </TextField>
                                          <VBox alignment="CENTER" prefHeight="200.0" prefWidth="634.0">
                                             <HBox.margin>
                                                <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                                             </HBox.margin>
                                             <children>
                                                <Text layoutX="10.0" layoutY="55.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MEMBER ID" wrappingWidth="178.884765625">
                                                   <VBox.margin>
                                                      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                                   </VBox.margin>
                                                </Text>
                                                <Text strokeType="OUTSIDE" strokeWidth="0.0" text="MEMBER NAME" wrappingWidth="178.08837890625">
                                                   <VBox.margin>
                                                      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                                   </VBox.margin>
                                                </Text>
                                                <Text layoutX="10.0" layoutY="23.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MEMBER EMAIL" wrappingWidth="180.43017578125">
                                                   <VBox.margin>
                                                      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                                   </VBox.margin>
                                                </Text>
                                                <Text layoutX="10.0" layoutY="39.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MEMBER CONTACT" wrappingWidth="180.6640625">
                                                   <VBox.margin>
                                                      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                                   </VBox.margin>
                                                </Text>
                                             </children>
                                          </VBox>
                                       </children>
                                    </HBox>
                                    <HBox alignment="CENTER" prefHeight="243.0" prefWidth="802.0">
                                       <children>
                                          <Button mnemonicParsing="false" styleClass="action-button" stylesheets="@homestyle.css" text="Issue">
                                             <font>
                                                <Font name="DejaVu Sans Mono Bold" size="30.0" />
                                             </font>
                                          </Button>
                                       </children>
                                    </HBox>
                                 </children>
                              </VBox>
                           </children></AnchorPane>
                  </content>
                </Tab>
                <Tab text="Renew ">
                  <content>
                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                           <children>
                              <BorderPane prefHeight="642.0" prefWidth="802.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                                 <top>
                                    <TextField alignment="CENTER" prefHeight="51.0" prefWidth="50.0" promptText="Enter Book ID" BorderPane.alignment="CENTER">
                                       <BorderPane.margin>
                                          <Insets left="150.0" right="150.0" />
                                       </BorderPane.margin>
                                    </TextField>
                                 </top>
                                 <center>
                                    <ListView prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
                                       <padding>
                                          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                       </padding>
                                    </ListView>
                                 </center>
                                 <bottom>
                                    <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
                                       <children>
                                          <Button alignment="CENTER" mnemonicParsing="false" styleClass="action-button" stylesheets="@homestyle.css" text="Return">
                                             <HBox.margin>
                                                <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                                             </HBox.margin>
                                             <padding>
                                                <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                             </padding>
                                          </Button>
                                          <Button mnemonicParsing="false" styleClass="action-button" stylesheets="@homestyle.css" text="Renew">
                                             <HBox.margin>
                                                <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
                                             </HBox.margin>
                                             <padding>
                                                <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                                             </padding>
                                          </Button>
                                       </children>
                                    </HBox>
                                 </bottom>
                              </BorderPane>
                           </children></AnchorPane>
                  </content>
                </Tab>
              </tabs>
            </TabPane>
         </center>
      </BorderPane>
   </children>
</StackPane>

HomeController.java -

package Home;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;


public class HomeController {

@FXML
void loadAddBooks(ActionEvent event) {
    loadWindow("/src/AddMember/AddMember.fxml","HEY");

}

@FXML
void loadAddMembers(ActionEvent event) {
    loadWindow("/src/AddBook/AddBook.fxml","HEY");
}

@FXML
void loadViewBooks(ActionEvent event) {
    loadWindow("/src/DisplayBook/DisplayBook.fxml","HEY");
}

@FXML
void loadViewMembers(ActionEvent event) {
    loadWindow("src/DisplayMembers/DisplayMembers.fxml","HEY");
}

void loadWindow(String loc, String title) {
    try {
        Parent parent = FXMLLoader.load(Objects.requireNonNull(getClass().getClassLoader().getResource(loc)));
        Stage stage = new Stage(StageStyle.DECORATED);
        stage.setTitle(title);
        stage.setScene(new Scene(parent));
        stage.show();
    } catch (IOException ex) {
        Logger.getLogger(HomeController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

HomeMain.java -

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

public class HomeMain extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Home.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args){
        launch(args);
    }
}

enter image description here

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Your FXML paths are incorrect. If you use `getClass().getClassloader().getResource(...)`, I think the paths are relative to default package (so get rid of `src`). If you use `getClass().getResource(...)`, then paths will be relative to class' package – julien.giband May 26 '21 at 14:18
  • @julien.giband *"If you use getClass().getResource(...), then paths will be relative to class' package*". Note that *relative* paths (not beginning `/`) will be relative to the current class. *Absolute* paths (beginning `/`) will be relative to the classpath. – James_D May 26 '21 at 14:33
  • @James_D thanks for the intel. I was looking for more precise information. Reason why I have not risked giving an actual answer with this. I always use `Class.getResource` with relative paths to avoid these questions. – julien.giband May 26 '21 at 14:39
  • @julien.giband I prefer `Class.getResource()`, because `ClassLoader.getResource()` appears to sometimes have issues (which I don't fully understand) when working with modular projects. But relative paths are only possible if the resources is in the same package as, or a subpackage of, the specified class. So sometimes absolute paths are necessary. The linked Q&A has all the gory details. – James_D May 26 '21 at 14:42
  • Given your project structure (which is a good one), a cleaner approach might be, e.g. `FXMLLoader.load(AddMember.class.getResource("AddMember.fxml"))`. (You could modify the `loadWindow` method to take an additional parameter of type `Class>`). – James_D May 26 '21 at 14:47
  • 1
    @James_D The issues might be due to this documentation from `ClassLoader#getResource(String)`: "_Additionally, and except for the special case where the resource has a name ending with `".class"`, this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource)_". Though why it's implemented that way I'm not entirely sure. – Slaw May 26 '21 at 18:53

0 Answers0