JavaFxでチェックボタンを押すとラベルに画像が表示されるウインドウを作っていますが、実行すると以下のエラーが出てきます。画像の問題かなと自分では思っています。
画像のとおり、Eclipse内で、同じsrc内に、1414406.jpgを置いて、引数の部分も1414406.jpgと書き直して実行したのですが、エラーが出て表示されません。追記になりますがよろしくお願いします。
エラーメッセージ
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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1128)
at javafx.scene.image.Image.<init>(Image.java:706)
at lesson4.SampleP2.start(SampleP2.java:29)
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
Exception running application lesson4.SampleP2
該当のコードは以下の通りです。
package lesson4;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class SampleP2 extends Application {
private Label lb;
private CheckBox ch;
private Image im;
private ImageView iv;
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage)throws Exception
{
Label lb = new Label("This is me");
CheckBox ch = new CheckBox("画像の表示");
im = new Image(getClass().getResourceAsStream("file:///C:/Users/USER/Pictures/1414406%20.jpg"));
iv = new ImageView(im);
lb.setGraphic(new ImageView(im));
BorderPane bp = new BorderPane();
bp.setCenter(lb);
bp.setBottom(ch);
lb.setAlignment(Pos.CENTER);
bp.setCenter(lb);
//イベントハンドラの登録
ch.setOnAction(new SampleEventHandler());
Scene sc =new Scene(bp,500,500);
stage.setScene(sc);
stage.setTitle("サンプル");
stage.show();
}
class SampleEventHandler implements EventHandler<ActionEvent>
{
public void handle(ActionEvent e) {
CheckBox tmp = (CheckBox)e.getSource();
if(tmp.isSelected() == true) {
lb.setGraphic(iv);
}
else if(tmp.isSelected() == false) {
lb.setGraphic(null);
}
}
// TODO 自動生成されたメソッド・スタブ