应用程序启动方法 java.lang.reflect.InvocationTargetException JavaFX 图像转换中的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44558929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in Application start method java.lang.reflect.InvocationTargetException JavaFX image transition
提问by Kaw123
When i try to add image transition for my JavaFX app, it has thrown this error,I checked the previous questions and answers here in Stackoverflow, but all the solutions were specific for each of those questions only.I am using netbeans IDE.
当我尝试为我的 JavaFX 应用程序添加图像转换时,它抛出了这个错误,我在 Stackoverflow 中检查了以前的问题和答案,但所有的解决方案都是针对每个问题的。我使用的是 netbeans IDE。
Executing H:\thirdf\FinalF\dist\run2135548605\FinalF.jar using platform C:\Program Files\Java\jdk1.8.0_101\jre/bin/java 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: javafx.fxml.LoadException: file:/H:/thirdf/FinalF/dist/run2135548605/FinalF.jar!/finalf/FXMLDocument.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 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 finalf.FinalF.start(FinalF.java:38) 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) at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ... 1 more 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 finalf.FXMLDocumentController.loadSplashScreen(FXMLDocumentController.java:55) at finalf.FXMLDocumentController.initialize(FXMLDocumentController.java:69) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) ... 17 more Exception running application finalf.FinalF Java Result: 1
使用平台 C:\Program Files\Java\jdk1.8.0_101\jre/bin/java 执行 H:\thirdf\FinalF\dist\run2135548605\FinalF.jar 应用程序启动方法中的异常 java.lang.reflect.InvocationTargetException at sun。反射.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) 在 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 在 com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 在 sun.reflect.NativeMethodAccessorImpl .invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun。反射.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 引起:java .lang.RuntimeException: com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 的应用程序启动方法异常,com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) ) 在 java.lang.Thread.run(Thread.java:745) 引起:javafx.fxml.LoadException: file:/H:/thirdf/FinalF/dist/run2135548605/FinalF.jar!/finalf/FXMLDocument.fxmljava:767) 引起:java.lang.RuntimeException:com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 处的应用程序启动方法异常,位于 com.sun.javafx.application.LauncherImpl.lambda$ launchApplication$155(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:745) 由:javafx.fxml.LoadException: file:/H:/thirdf/FinalF/dist/run2135548605/FinalF.jar !/finalf/FXMLDocument.fxmljava:767) 引起:java.lang.RuntimeException:com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 处的应用程序启动方法异常,位于 com.sun.javafx.application.LauncherImpl.lambda$ launchApplication$155(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:745) 由:javafx.fxml.LoadException: file:/H:/thirdf/FinalF/dist/run2135548605/FinalF.jar !/finalf/FXMLDocument.fxml/thirdf/FinalF/dist/run2135548605/FinalF.jar!/finalf/FXMLDocument.fxml/thirdf/FinalF/dist/run2135548605/FinalF.jar!/finalf/FXMLDocument.fxml
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) at com.sun.glass.ui.win.WinApplication.lambda$null $148(WinApplication.java:191) ... 1 个原因:java.lang.NullPointerException:需要位置。在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207) 在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 在 javafx.fxml。 FXMLLoader.loadImpl(FXMLLoader.java:3124) 在 javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 在 javafx.fxml.FXMLLoader。
here's the code
这是代码
SplashFXML.fxml
SplashFXML.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.StackPane?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="574.0" xmlns="http://javafx.com/javafx/8.0.101" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fitHeight="450.0" fitWidth="560.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@loader.png" />
</image>
</ImageView>
</children>
</StackPane>
FXMLDocument.fxml
FXML文档.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="462.0" prefWidth="473.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.101" fx:controller="finalf.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="126.0" layoutY="225.0" onAction="#handleButtonAction" prefHeight="31.0" prefWidth="212.0" text="Play" />
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<Button layoutX="126.0" layoutY="312.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="212.0" text="Instructions" />
<TextField layoutX="126.0" layoutY="141.0" prefHeight="31.0" prefWidth="212.0" />
</children>
</AnchorPane>
FXMLDocumentController.java
文件控制器.java
package finalf;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.FadeTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private AnchorPane root;
@FXML
private void handleButtonAction(ActionEvent event) {
try{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Second_Window.fxml"));
Parent rootl = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle("Foker");
stage.setScene(new Scene(rootl));
stage.show();
}catch(Exception e){
System.out.println("Can't load new window");
}
}
private void loadSplashScreen() throws IOException{
StackPane pane = FXMLLoader.load(getClass().getResource("/SplashFXML.fxml"));
root.getChildren().setAll(pane);
FadeTransition fadeIn = new FadeTransition(Duration.seconds(3),pane);
fadeIn.setFromValue(0);
fadeIn.setToValue(1);
fadeIn.setCycleCount(1);
fadeIn.play();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
loadSplashScreen();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
FinalF.java
最终F.java
package finalf;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class FinalF extends Application {
@FXML
private Button button;
@FXML
private Label label;
@FXML
void handleButtonAction(ActionEvent event){
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
回答by yab
let's try
我们试试吧
- in FXMLDocumentController.java, change the first line in
loadSplashScreen()
method,
- 在FXMLDocumentController.java 中,更改
loadSplashScreen()
方法中的第一行,
from
从
StackPane pane = FXMLLoader.load(getClass().getResource("/SplashFXML.fxml"));
to
到
StackPane pane = FXMLLoader.load(getClass().getResource("SplashFXML.fxml"));
- in FXMLDocument.fxml, add
fx:id="root"
to theAnchorPane
section. make it change
- 在FXMLDocument.fxml 中,添加
fx:id="root"
到该AnchorPane
部分。让它改变
from
从
AnchorPane id="AnchorPane" prefHeight="462.0" prefWidth="473.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.101" fx:controller="finalf.FXMLDocumentController"
to
到
AnchorPane id="AnchorPane" fx:id="root" prefHeight="462.0" prefWidth="473.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.101" fx:controller="finalf.FXMLDocumentController"
Hope it solves your problem after this two steps.
希望这两个步骤后它可以解决您的问题。