2014-01-15 33 views
5

我越來越位置需要使用JavaFX的gradle與

顯示java.lang.NullPointerException:需要位置。

當我使用gradle與javafx插件組裝後運行程序時。如果我從IntelliJ Idea運行它,一切都可以。 Java源文件和.fxml位於某個包中。

的build.gradle

apply plugin: 'java' 
apply plugin: 'application' 
apply plugin: 'idea' 
apply from: 'javafx.plugin' 

javafx { 
    javaRuntime = 'C:\\Program Files\\Java\\jdk1.7.0_45' 

    appID 'FXMLExample' 
    appName 'fxml example application' 
    mainClass 'local.hz.FXMLExample' 
} 

task "create-dirs" { 
    sourceSets*.java.srcDirs*.each {it.mkdirs()} 
    sourceSets*.resources.srcDirs*.each {it.mkdirs()} 
} 

fxml_example.fxml

<?xml version="1.0" encoding="UTF-8"?> 
<?import java.net.*?> 
<?import javafx.geometry.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.*?> 

<GridPane fx:controller="local.hz.FXMLExampleController" 
     xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10"> 
<padding><Insets top="25" right="25" bottom="10" left="25"/></padding> 
<gridLinesVisible>true</gridLinesVisible> 

<Text text="Welcome" 
     GridPane.columnIndex="0" GridPane.rowIndex="0" 
     GridPane.columnSpan="2"/> 

<Label text="User Name:" 
     GridPane.columnIndex="0" GridPane.rowIndex="1"/> 

<TextField 
     GridPane.columnIndex="1" GridPane.rowIndex="1"/> 

<Label text="Password:" 
     GridPane.columnIndex="0" GridPane.rowIndex="2"/> 

<PasswordField fx:id="passwordField" 
       GridPane.columnIndex="1" GridPane.rowIndex="2"/> 

<HBox spacing="10" alignment="bottom_right" 
     GridPane.columnIndex="1" GridPane.rowIndex="4"> 
    <Button text="Sign In" 
      onAction="#handleSubmitButtonAction"/> 
</HBox> 

<Text fx:id="actiontarget" 
     GridPane.columnIndex="1" GridPane.rowIndex="6"/> 
</GridPane> 

FXMLExample.java

package local.hz; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
public class FXMLExample extends Application { 
@Override 
public void start(Stage stage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml")); 

    Scene scene = new Scene(root,300,275); 

    stage.setTitle("FXML WELCOME!!"); 
    stage.setScene(scene); 
    stage.show(); 
} 

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

回答

-1

嘗試更換:

javaRuntime = 'C:\\Program Files\\Java\\jdk1.7.0_45' 

有:

javaRuntime = 'C:/Program Files/Java/jdk1.7.0_45' 
+0

沒有幫助 –

+0

這是相同的路徑。 –

16

你FXML文件可能不是打包成你的罐子。

嘗試增加了此信息的build.gradle文件:

sourceSets { 
    main { 
    resources { 
     srcDirs = ["src/main/java"] 
     includes = ["**/*.fxml"] 
    } 
    } 
} 
+2

這個答案是正確的。問題的作者可以接受它。 –

+0

謝謝@KonradG。爲我工作。 –

+0

如果你把'* .fxml'放在你的代碼包下,你應該寫'srcDirs = [「path/to/your/code/root /」]' –

0

其實,裝載資源與搖籃可能是從使用IDE資源系統不同。

這是更好地用classloader:

URL myFxmlURL = ClassLoader.getSystemResource("fxml_example.fxml"); 
FXMLLoader loader = new FXMLLoader(sampleUrl); 

Parent root = loader.load(myFxmlURL);