【问题标题】:JavaFX Image getting image pathJavaFX Image获取图像路径
【发布时间】:2013-08-02 11:58:54
【问题描述】:

我有以下代码:

package imagebrowser;

import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author Dess
 */
public class ImageBrowser extends Application {
    Image[] images;
    ImageView[] imagesView;

@Override
public void start(Stage primaryStage) {


    StackPane root = new StackPane();

    Scene scene = new Scene(root, 600, 450);

    int j = 0;

    String path = "C://imbr/";
    String files;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
        if (listOfFiles[i].getName().endsWith("jpg") || listOfFiles[i].getName().endsWith("JPG")) {
            System.out.println(listOfFiles[i].getAbsolutePath());

            files = "file:" + listOfFiles[i].getAbsolutePath();
            System.out.println(files);

            images[j] = new Image(files, 200, 200, true, true);
            imagesView[j] = new ImageView();
            imagesView[j].setImage(images[j]);
            j++;

        }
    }

    primaryStage.setTitle("Przegladarka Obrazkow");
    primaryStage.setScene(scene);
    for(int i = 0; i < imagesView.length; i++){
    root.getChildren().add(imagesView[i]);
    }
    primaryStage.show();

}

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

}

这不起作用:

   Executing com.javafx.main.Main from C:\Users\Dess\Documents\NetBeansProjects\ImageBrowser\dist\run1928942616\ImageBrowser.jar using platform C:\Program Files\Java\jdk1.7.0_17/bin/java
C:\imbr\1.jpg
file:C:\imbr\1.jpg
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.javafx.main.Main.launchApp(Main.java:642)
    at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at imagebrowser.ImageBrowser.start(ImageBrowser.java:49)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    ... 1 more
Java Result: 1

我知道这与图像文件的路径有关,但我不知道为什么。 我在网站上找到了使用这种方式确定路径的示例,并且它有效...... 我会感谢一些帮助^^

【问题讨论】:

标签: java image javafx


【解决方案1】:

原因是java.lang.NullPointerException,因为你还没有初始化你的数组变量:

Image[] images;            // images is null
ImageView[] imageViews;    // imageViews is also null

访问images时喜欢

images[j] = new Image(files, 200, 200, true, true);

NullPointerException 被抛出,因为 imagesnull

要做到这一点,你需要创建一个对应的数组对象,比如

Image[] images = new Image[100];    // now images is a reference to an array of 100 elements

但是,我强烈不建议您为此使用普通数组 - 您不知道要提前加载的图像数量(但您知道最大值,因此您仍然可以分配一个足够大的数组来容纳最大数量的图像,但这可能会浪费内存),所以使用ArrayList之类的东西:

List<Image> images = new ArrayList<>();

...
images.add(new Image(files, 200, 200, true, true));
...

另见Why is it preferred to use Lists instead of Arrays in Java?

【讨论】:

    猜你喜欢
    • 2014-09-27
    • 2015-02-19
    • 2016-01-23
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多