0

I'm trying to make an application for a college work using Maven, JavaFX, Persistence and Hibernate. I had a fully working code that used persistence, maven and hibernante with command line, and I just needed to make it work with a JavaFX interface (I couldn't find a Maven version that worked with the versions I had of Hibernate and Persistence, so I just manually added JavaFX instead of using Maven). For that, I followed a tutorial on JavaFX, and then I got myself a fully functioning but empty interface. So all I needed was to implement this interface into the Hibernate project. The problem is: Whenever I try to run my main application, I get the following error:

Exception in Application start method 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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.IllegalStateException: Location is not set. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) at principal.Aplicacao.iniciarRootLayout(Aplicacao.java:72) at principal.Aplicacao.start(Aplicacao.java:51) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191) ... 1 more Exception running application principal.Aplicacao

It seems like there is something work with my Start() method. The following is the code that is in the main app class (called "Aplicacao"):

package principal; import java.io.IOException; import java.util.List; import java.util.Scanner;

import dao.ProdutoDAO; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.BorderPane; import javafx.stage.Modality; import javafx.stage.Stage; import modelo.Produto; import view.ProdutoEditarDialogoControlador; import view.ProdutoVisaoGeralControlador;

public class Aplicacao extends Application {     private Stage primaryStage;     private BorderPane rootLayout;      private ObservableList<Produto> produtoData = FXCollections.observableArrayList();      private static ProdutoDAO dao;
         public Aplicacao()      {   }
      

        public ObservableList<Produto> getProdutoData()         {           return produtoData;         }
                public static ProdutoDAO getDao()       {           return dao;         }
            @Override
        public void start(Stage primaryStage){          this.primaryStage = primaryStage;
            this.primaryStage.setTitle("Estoque de Produtos usando Hibernate e JavaFX");
            this.primaryStage.getIcons().add(new Image("file:resources/images/iconfinder_Warehouse_32.png"));
            
            

            iniciarRootLayout();
            
           // dao = new ProdutoDAO();
            

           // mostrarProdutoVisaoGeral();
            
           // List<Produto> lista = dao.getTodos();
                        //for (int i = 0; i < (lista.size() - 1); i++)              //{             //  produtoData.add(new Produto(lista.get(i).getId(), lista.get(i).getNome()));             //}
        }
         public void iniciarRootLayout()     {
            try 
            {
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(Aplicacao.class.getResource("view/RootLayout.fxml"));
                rootLayout = (BorderPane) loader.load();
                
                Scene scene = new Scene(rootLayout);
                primaryStage.setScene(scene);
                primaryStage.show();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }    }
         public void mostrarProdutoVisaoGeral()      {       try         {
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(Aplicacao.class.getResource("view/ProdutoVisaoGeral.fxml"));
                AnchorPane produtoVisaoGeral = (AnchorPane) loader.load();

                rootLayout.setCenter(produtoVisaoGeral);

                ProdutoVisaoGeralControlador controller = loader.getController();
                controller.setMainApp(this);

         }           catch (IOException e)           {
                e.printStackTrace();         }   }
         public Stage getPrimaryStage()         {           return primaryStage;        }
         public boolean mostrarProdutoDialogoEditar(Produto produto)     {
            try 
            {
                FXMLLoader loader = new FXMLLoader();
                loader.setLocation(Aplicacao.class.getResource("view/PersonEditDialog.fxml"));
                AnchorPane page = (AnchorPane) loader.load();

                Stage dialogStage = new Stage();
                dialogStage.setTitle("Editar Produto");
                dialogStage.initModality(Modality.WINDOW_MODAL);
                dialogStage.initOwner(primaryStage);
                Scene scene = new Scene(page);
                dialogStage.setScene(scene);

                ProdutoEditarDialogoControlador controller = loader.getController();
                controller.setDialogStage(dialogStage);
                controller.setProduto(produto);

                dialogStage.showAndWait();

                return controller.isOkClicked();
            } 
            
            catch (IOException e) 
            {
                e.printStackTrace();
                return false;
            }       }       

public static void main(String[] args) { launch(args); }

enricogp
  • 447
  • 2
  • 5
  • 9
  • Duplicate chosen based on: `Caused by: java.lang.NullPointerException: location is not set` – Slaw Dec 05 '20 at 22:13
  • Do you know how to read a stack trace? – Thorbjørn Ravn Andersen Dec 05 '20 at 22:30
  • Not really. Do you know any sources I could read so that I can learn how to correctly read one? Also, I'm not sure why there would be a problem in determining the path for fxml files. I have a project I created to learn JavaFX that apparently calls those files in the same exact way and no errors appear there. – enricogp Dec 05 '20 at 22:57
  • The answer to the duplicate goes into a lot of detail and should be able to tell you where something went wrong. As for reading stack traces, see [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). And for future reference, you should format stack traces as code; they're much easier to read that way. – Slaw Dec 06 '20 at 00:33
  • Though reading your question again, I don't understand "_I couldn't find a Maven version that worked with the versions I had of Hibernate and Persistence, so I just manually added JavaFX instead of using Maven_". Just use the latest version of Maven and it should work fine. If you're having trouble adding JavaFX to your Maven project then check out [Getting Started with JavaFX](https://openjfx.io/openjfx-docs/). – Slaw Dec 06 '20 at 00:37
  • I'm using old versions of Persistence and Hibernate that are only compatible with old java versions (those are the versions our teacher assigned to us). Newer javaFX versions (the ones you can find in maven repositories) require newer java jdk that won't work with those specific Persistence and Hibernate. – enricogp Dec 06 '20 at 15:05

0 Answers0