2017-11-14 11 views
0

Gluon/JavaFX를 처음 사용하려고하지만 런타임에 InvocationTargetException이 발생합니다. 이 사이트와 다른 사이트에서 답변을 검색했지만 각 제안을 시도한 후에도 동일한 문제가 발생했습니다.JavaFX를 사용할 때 InvocationTargetException이 발생했습니다.

홈페이지 :

package Scenes; 
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 Main extends Application{ 


    public void start(Stage stage) throws Exception{ 

     Parent root = FXMLLoader.load(getClass().getResource("test.fxml")); 
     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.setTitle("asdf"); 
     stage.show(); 
    } 

    public static void main(String[] args) { 

     launch(args); 
    } 



} 

컨트롤러 :

package Scenes; 
import java.io.File; 

import javax.swing.JButton; 
import javax.swing.JFileChooser; 

import javafx.scene.control.Button; 


public class Controller { 

    Button kButton; 
    Button bpButton; 

    public void initialize() { 

    } 

    public static void browseFileK() { 
     JFileChooser jfc = new JFileChooser(); 
     int returnValue = jfc.showOpenDialog(null); 
     if(returnValue == JFileChooser.APPROVE_OPTION) 
     { 
      File selectedFile = jfc.getSelectedFile(); 
     } 
    } 

     public static void browseFileBP() { 
      JFileChooser jfc = new JFileChooser(); 
      int returnValue = jfc.showOpenDialog(null); 
      if(returnValue == JFileChooser.APPROVE_OPTION) 
      { 
       File selectedFile = jfc.getSelectedFile(); 
      } 
     } 

} 

FXML 파일 :

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

<?import com.gluonhq.charm.glisten.control.TextField?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.text.Font?> 

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.151" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> 
    <children> 
    <AnchorPane cache="true" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS"> 
     <children> 
     <Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style="&#10;" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false"> 
      <font> 
      <Font size="18.0" /> 
      </font> 
     </Label> 
      <TextField layoutX="21.0" layoutY="47.0" prefHeight="26.0" prefWidth="237.0" text="Select location of Kasasa accounts file" /> 
      <TextField layoutX="21.0" layoutY="118.0" prefHeight="26.0" prefWidth="237.0" text="Select location of Bill Pay accounts file" /> 
      <Button layoutX="294.0" layoutY="48.0" mnemonicParsing="false" text="Browse..." /> 
      <Button layoutX="294.0" layoutY="115.0" mnemonicParsing="false" text="Browse..." /> 
      <Button layoutX="168.0" layoutY="269.0" mnemonicParsing="false" prefHeight="32.0" prefWidth="253.0" text="Generate refund document" /> 
     </children> 
    </AnchorPane> 
    </children> 
</VBox> 

스택 트레이스 :

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    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(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 
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$154(LauncherImpl.java:182) 
    at java.lang.Thread.run(Unknown Source) 
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 Scenes.Main.start(Main.java:14) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177) 
    ... 1 more 

fxml 파일을 main과 같은 패키지에 추가하고 fxml 파일 앞에 "/"를 추가하거나 파일 이름을 "Scenes/test.fxml"로 변경하는 것이 가장 일반적으로 권장되는 것 같습니다. 나를 위해 일 해왔다.

+0

실제 오류 및 스택 추적을 포함 할 수 있습니까? – MMAdams

+0

나는 당신이'IDE '를 사용하고 있지 않다고 추측 할 것이다. 'NetBeans' 나'Eclipse'를 다운로드하라고 제안 할 것입니다. – Sedrick

+0

Eclipse를 사용하고 있습니다. 이것은 JavaFX 사용에 대한 나의 첫 번째 시도이며 전문 프로그래머가 아닙니다. –

답변

2

귀하의 질문에 대한 답변은 빌드 시스템이하는 일에 상당히 의존합니다. 예를 들어 일부 시스템을 빌드하면 소스 폴더 대신 resources 폴더에없는 경우 .fxml 파일을 빌드 디렉토리에 복사하지 않습니다. 나머지는 빌드 폴더에 복사 할 파일 패턴을 명시 적으로 지정해야합니다.

가장 빠른 방법은 빌드 폴더 (출력 디렉토리)를 확인하고 .fxml 파일이 있는지 확인하는 것입니다.

fx:controller="sample.Controller" 

하지만 Controller 당신은 Swing 구성 요소를 사용하는

fx:controller="Scenes.Controller" 

그래서 FXML해야

package Scenes; 

에 있습니다

+0

fxml 파일이 내 bin 폴더에 표시됩니다. –

+2

정확히 어디입니까? 'getClass (...). getResource (resource)'를 사용하면 클래스 로더는 클래스의 패키지에 해당하는 폴더에서 파일을 검색합니다. 귀하의 경우, 그것은 "Controller.class' 파일과 동일한 위치에"Scene "하위 디렉토리에 있어야합니다. –

+0

또한 @SedrickJefferson이 지적했듯이 올바른 컨트롤러 클래스를 말하는 것이 아닙니다. 'sample.Controller'가 아닌'Scenes.Controller' 여야합니다. –

0

귀하의 FXML 파일 말한다.

import javax.swing.JButton; 
import javax.swing.JFileChooser; 

JavaFx 구성 요소를 사용해야합니다.

import javafx.scene.control.Button; 
import javafx.stage.FileChooser; 
+2

사실이긴하지만 오류가 발생하는 이유는 아닙니다. –

+0

사실, 'Button'이 오류의 원인입니다. 그는'Swing'' FileChooser를 사용하여 도망 갈 수 있습니다. – Sedrick

+0

아마도 그렇지 않을 수도 있습니다. 나는 이것이 FXML 버튼이라고 생각했다. – Sedrick