这是我在Netbeans程序中到目前为止的代码。它应该打开一个带有图片的窗口,这张照片叫做seawave。但由于某种原因,它没有运行。你知道为什么吗?
package myseaapp;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class MySeaApp extends Application {
private ImageView seawave;
private Rectangle seawaveClip;
@Override
public void start(Stage primaryStage) {
primaryStage.initStyle(StageStyle.TRANSPARENT);
seawave = new ImageView(new Image(MySeaApp.class.getResourceAsStream("images/seawave")));
seawaveClip = new Rectangle(300, 220);
seawaveClip.setArcHeight(20);
seawaveClip.setArcWidth(20);
seawave.setClip(seawaveClip);
Pane root = new Pane();
root.getChildren().add(seawave);
Scene myScene = new Scene(root, 300, 250);
myScene.setFill(null);
primaryStage.setScene(myScene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这就是我得到的......
Executing /Users/SethRataiczak/NetBeansProjects/MySeaApp/dist/run1104410607/MySeaApp.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/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: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.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1110)
at javafx.scene.image.Image.<init>(Image.java:694)
at myseaapp.MySeaApp.start(MySeaApp.java:30)
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)
Exception running application myseaapp.MySeaApp
Java Result: 1
Deleting directory /Users/SethRataiczak/NetBeansProjects/MySeaApp/dist/run1104410607
jfxsa-run:
BUILD SUCCESSFUL (total time: 1 second)
答案 0 :(得分:0)
首先使用try和catch块进行错误处理,如下所示
try {
// code
}catch {
// error handling code
}
和第二个你没有使用你的图像类型示例.png,.jpg等
seawave = new ImageView(new Image(MySeaApp.class.getResourceAsStream("images/seawave")));
使用正确的图像类型更改此行代码
像这样:
seawave = new ImageView(new Image(MySeaApp.class.getResourceAsStream("images/seawave.jpg")));