这是我在Netbeans程序中迄今为止的代码。它应该打开一扇窗户,里面有一幅画,这幅画被称为海浪。但由于某种原因,它没有运行。你知道为什么吗?
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)
首先使用try-and-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")));