2017-10-02 3 views
0

J'ai suivi these steps to create a maven project with JavaFX in intelliJ.Projet JavaFX avec Maven sur ExelliJ Runtime Exception

Mais, en cours d'exécution des résultats du projet dans un Runtime:

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:498) 
    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:498) 
    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$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:745) 
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 Valor.CM.Main.start(Main.java:13) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 

J'ai essayé la recherche et la modification de la classe principale, mais rien ne semble fonctionner jusqu'à présent.

Main.java:

package Valor.CM; 

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

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     Parent root = FXMLLoader.load(Main.class.getResource("sample.fxml")); 
     primaryStage.setTitle("Hello World"); 
     primaryStage.setScene(new Scene(root, 300, 275)); 
     primaryStage.show(); 
    } 


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

Controller.java:

package Valor.CM; 

public class Controller { 
} 

sample.fxml:

<?import javafx.geometry.Insets?> 
<?import javafx.scene.layout.GridPane?> 

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<GridPane fx:controller="Valor.CM.Controller" 
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10"> 
</GridPane> 

pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>Valor.CM</groupId> 
    <artifactId>StopServiceTester</artifactId> 
    <version>1.0-SNAPSHOT</version> 


</project> 
+2

Votre chemin sample.fxml est incorrect: at Valor.CM.Main.start (Main.java:13): – duffymo

+0

Quel devrait être le bon chemin? À l'heure actuelle, il se trouve au même emplacement que les classes Main et Controller. –

+1

La question que j'ai posée n'est plus pertinente pour moi car j'ai recréé le projet en utilisant une méthode différente. J'ai simplement exécuté cette commande et importé le projet mvn à intelliJ: 'mvn archetype: générer -DarchetypeGroupId = com.zenjava -DarchetypeArtifactId = javafx-basic-archetype' –

Répondre

0

Puisque vous utilisez

Main.class.getResource("sample.fxml") 

pour trouver la ressource. Vous devez déplacer le sample.fxml dans un dossier nommé resources sous le répertoire src > main de votre projet.

+0

J'ai déplacé le fichier, mais j'ai toujours la même exception. –

+0

@AlexWeitz Vous devez vous assurer que 'Main.class' se trouve dans le dossier' src> main> java' et que de la même manière le fichier sample.fxml se trouve dans 'src> main> resource'. Une autre façon de déboguer ceci est d'utiliser le chemin complet vers la ressource de votre répertoire comme '/ Users/some/dossier/un/autre/chemin/vers/sample.fxml' que vous pouvez obtenir par la propriété du fichier sample.fxml. – nullpointer

+1

Je suis désolé, je ne pouvais pas résoudre le problème et finalement allé avec une méthode différente pour créer un projet: 'mvn archetype: générer -DarchetypeGroupId = com.zenjava -DarchetypeArtifactId = javafx-basic-archetype' –