Error after running in javafx

Asked 2 years ago, Updated 2 years ago, 140 views

When I press the check button in JavaFx, I create a window where the image is displayed on the label, but when I run it, I get the following error:I think it might be a problem with the image.

As you can see in the image, I put 1414406.jpg in the same src and rewritten the argument as 1414406.jpg, but the error did not appear.Thank you for your cooperation.

src=

Error Message

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke
    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
    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)
    atlesson 4.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)
    atcom.sun.javafx.application.PlatformImpl.lambda$runLater$173 (PlatformImpl.java:294)
    atcom.sun.class.ui.InvokeLaterDispatcher $Future.run (InvokeLaterDispatcher.java:95)
    atcom.sun.class.ui.win.WinApplication._runLoop (Native Method)
    at com.sun.class.ui.win.WinApplication.lambda$null$147 (WinApplication.java:177)
    ... 1 more
Exception running application lesson 4.SampleP2

The affected codes are as follows:

packagelesson4;

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 extensions Application {
    private label lb;
    private CheckBoxch;
    private Image im;
    private ImageViewiv;

    public static void main(String[]args) {
        launch(args);
    }

    public void start (Stage stage) threads Exception
    {
        Label lb = new Label ("This is me");
        CheckBoxch = new CheckBox("View Image");
        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);
        // Register Event Handler
        ch.setOnAction(newSampleEventHandler());

        Scenesc = new Scenes(bp,500,500);

        stage.setScene(sc);

        stage.setTitle("sample");
        stage.show();
    }

    classSampleEventHandler implements EventHandler<ActionEvent>
    {
        public void handle (ActionEvente) {
            CheckBox tmp=(CheckBox)e.getSource();
            if(tmp.isSelected()==true){
                lb.setGraphic(iv);
            }
            else if(tmp.isSelected()==false){
                lb.setGraphic (null);
            }
        }
        // TODO AUTOMATICALLY GENERATED METHOD STUB

java javafx

2022-09-30 16:47

1 Answers

You specified the wrong resource name.

Specifically, this part

im=new Image(getClass().getResourceAsStream("file://C:/Users/USER/Pictures/1414406%20.jpg"));

In , the argument should be resource name, not filename (name on file system).

As an example of a solution, place the file named 1414406%20.jpg in the same location as the SampleP2.class that resides on the class path, and then

im=new Image(getClass().getResourceAsStream("1414406%20.jpg"));

By rewriting it to , you will be able to see the image.

Perhaps there is a reference to the reference document about where the image is stored, so please read it again.

(Additional)

For screenshot packaging configurations, "/lesson5/1414406.jp".


2022-09-30 16:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.