尝试在 JavaFX 中显示图像时输入流不能为空错误

问题描述 投票:0回答:0

我有一个项目,访客可以以图书管理员或用户身份登录,如果他以用户身份登录,他将看到数据库中所有书籍的图像列表,现在如果以图书管理员身份登录,他可以添加一个新的book 到数据库,然后他从他的计算机添加图像,我使用以下代码将它添加到我的 resources/images/books 文件夹:

//let the librarian choose an image to upload

    public void chooseImg(ActionEvent actionEvent) {
        FileChooser fileChooser= new FileChooser();
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif")
        );
        selectedFile = fileChooser.showOpenDialog(null);
    }
    //add book to the db

    public void addBook(ActionEvent actionEvent) throws SQLException{
        String title= bookTitle.getText();
        String cat= bookCat.getText();
        String desc= bookDesc.getText();
        String author= bookAuthor.getText();
        String length= bookLength.getText();
        String quantity= bookQuantity.getText();
        String imagePath = "images/books/" + selectedFile.getName();
        String destinationDirPath = "src/main/resources/images/books";

        try {
            // Create the destination directory if it does not exist
            Path destinationDir = Paths.get(destinationDirPath);
            if (!Files.exists(destinationDir)) {
                Files.createDirectories(destinationDir);
            }

            // Copy the selected file to the destination directory
            Path destinationPath = Paths.get(destinationDirPath, selectedFile.getName());
            Files.copy(selectedFile.toPath(), destinationPath, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            System.err.println("Failed to copy image: " + e.getMessage());
        }
        if(!title.isEmpty() && !author.isEmpty() && !length.isEmpty() && !cat.isEmpty() && !quantity.isEmpty() && !desc.isEmpty() && selectedFile != null){
            Book newbook= new Book(title,author,desc,cat,imagePath,length,""+proxyUser.getRealUser().getId());
            newbook.setQuantity(quantity);
            proxyUser.getRealUser().addBook(newbook);
            proxyUser.getRealUser().notifyUsers();
        }
    }

现在将图像添加到书籍文件夹中,然后我将用户填写的所有内容作为一本新书放入我的数据库中,其中图像采用这种格式: images/books/nameofimage.extention 例如:images/books/bookimg.png 现在,如果图书管理员注销,现在来宾再次以用户身份登录,此错误将显示在我的日志中:

Caused by: java.lang.NullPointerException: Input stream must not be null
    at javafx.graphics@19/javafx.scene.image.Image.validateInputStream(Image.java:1145)
    at javafx.graphics@19/javafx.scene.image.Image.<init>(Image.java:708)
    at com.example.project/com.example.project.iterator.GeneralIterator.show(GeneralIterator.java:72)
    at com.example.project/com.example.project.AllBooksController.getAllBooks(AllBooksController.java:76)
    at com.example.project/com.example.project.AllBooksController.start(AllBooksController.java:66)
    at com.example.project/com.example.project.LoginController.login(LoginController.java:81)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    ... 53 more

输入流不能为空,因为我的新图像没有在同一个应用程序实例中被识别,这是显示图像的代码:

ImageView imageView = new ImageView();
            Image image;
            image = new Image(getClass().getResourceAsStream("/" + bookImage));
            System.out.println("I am Here");
            imageView.setImage(image);
            imageView.setFitWidth(207);
            imageView.setFitHeight(300);

            Label label = new Label(bookTitle);
            label.setFont(Font.font("Corbel", 16));
            label.setTextFill(Color.web("#f0824f"));
            VBox newVbox = new VBox();
            newVbox.getChildren().add(imageView);
            newVbox.getChildren().add(label);
            newVbox.setPrefWidth(207);
            newVbox.setPrefHeight(320);
            newVbox.getStyleClass().add("book-vbox");

如何让它显示所有图像,包括新图像?

java javafx inputstream fxml
© www.soinside.com 2019 - 2024. All rights reserved.