JSON 对象的 Java 空指针异常

Java Null Pointer Exception for JSON Object

提问人:Ruben Odamo 提问时间:11/13/2021 更新时间:11/13/2021 访问量:2002

问:

这是我的代码,它在我的代码中名为“films”的特定 JSON 对象上抛出 java null 指针异常。我不明白为什么它认为这是“空”。在给出错误的地方,是代码应该根据方法“readJSONFile”读取txt文件的内容的地方。此方法将 txt 文件的元素添加到列表中。需要将要添加的新电影添加到 JSON 对象中,但这不起作用,因为电影显然是“空”的。

package application;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

import org.json.simple.JSONObject;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;

public class FilmManagement {
    
    private File selectedImage;
    
    @FXML
    private Button backBtn, homeBtn, uploadImageBtn, viewFilmsBtn, addFilmBtn;
    @FXML
    private Text newFilmTitle, newFilmDescription, newFilmTime1, newFilmTime2, newFilmTime3, newFilmAge, newFilmRating;
    @FXML
    private Label  newFilmStartDate, newFilmEndDate;
    @FXML
    private TextArea filmDescription;
    @FXML
    private DatePicker filmStartDate, filmEndDate;
    @FXML
    private TextField filmTitle, filmTrailer, filmRating;
    @FXML
    private ComboBox<String> filmTime1, filmTime2, filmTime3, filmAge;
    @FXML
    private ImageView uploadedFilmPoster;
    
    @FXML
    void initialize() throws IOException{
        ObservableList<String> obsList1 = FXCollections.observableArrayList("13:00", "14:00", "15:00", "16:00", "17:00",
                "18:00", "19:00", "20:00", "21:00", "22:00", "23:00", "00:00", "01:00", "02:00", "03:00");
        ObservableList<String> obsList2 = FXCollections.observableArrayList("U", "PG", "12A", "15", "18", "R");
        filmAge.setItems(obsList2);
        filmAge.setValue("12A");
        newFilmAge.setText("12A");
        filmTime1.setItems(obsList1);
        filmTime2.setItems(obsList1);
        filmTime3.setItems(obsList1);
        filmTime1.setValue("21:00");
        newFilmTime1.setText("21:00");
        filmStartDate.setValue(LocalDate.now());
        LocalDate startDate = filmStartDate.getValue();
        String startDateFormatted = startDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
        newFilmStartDate.setText(startDateFormatted);
    }
    
    public void goBack(ActionEvent event) throws IOException{
        Main m = new Main();
        m.changeScene("Employee Home.fxml");
    }
    
    public void goHome(ActionEvent event) throws IOException{
        Main m = new Main();
        m.changeScene("Employee Home.fxml");
    }
    
    public void goToViewFilms(ActionEvent event) throws IOException{
        Main m = new Main();
        m.changeScene("View Films.fxml");
    }
    
    //Method that gets called every time the user enters a date, time or age when adding a new movie.
    @FXML
    public void updateDateTimeAge(ActionEvent e) {

        try {
            switch (((Node) e.getSource()).getId()) {
            case "filmStartDate":
                LocalDate startDate = filmStartDate.getValue();
                String startDateFormatted = startDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
                newFilmStartDate.setText(startDateFormatted);
                break;
            case "filmEndDate":
                LocalDate endDate = filmEndDate.getValue();
                String endDateFormatted = endDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
                newFilmEndDate.setText(endDateFormatted);
                break;
            case "filmTime1":
                newFilmTime1.setText(filmTime1.getValue().toString());
                break;
            case "filmTime2":
                newFilmTime2.setText(filmTime2.getValue().toString());
                break;
            case "filmTime3":
                newFilmTime3.setText(filmTime3.getValue().toString());
                break;
            case "filmAge":
                newFilmAge.setText(filmAge.getValue().toString());
                break;
            }
        } catch (NullPointerException ex) {
            ex.getMessage();
        }
    }
    
    
    //Method that gets called every time the user types in any TextField when adding a movie
    @FXML
    public void updateFilmText(KeyEvent e) {

        switch (((Node) e.getSource()).getId()) {
        case "filmTitle":
            if (filmTitle.getText().length() > 20) {
                filmTitle.setEditable(false);
            }
            break;
        case "filmDescription":
            if (filmDescription.getText().length() > 220) {
                filmDescription.setEditable(false);
            }
            break;
        case "filmRating":
            //String ratingRegex = "^([1-9]\\d*|0)(\\.\\d)?$";
            //Pattern p = Pattern.compile(ratingRegex)
            if (Float.parseFloat(filmRating.getText()) > 10) {
                filmRating.setEditable(false);
            }
            break;
        }

        if (e.getCode().equals(KeyCode.BACK_SPACE)) {
            filmTitle.setEditable(true);
            filmDescription.setEditable(true);
            filmRating.setEditable(true);
        }

        switch (((Node) e.getSource()).getId()) {
        case "filmTitle":
            newFilmTitle.setText(filmTitle.getText());
            break;
        case "filmDescription":
            newFilmDescription.setText(filmDescription.getText());
            break;
        case "filmRating":
            newFilmRating.setText(filmRating.getText()+"/10");
            break;
        }
    }
    
    //Method that gets called when the user clicks on the upload image button
    @FXML
    public void uploadImage(ActionEvent event) throws IOException {

        try {
            FileChooser fc = new FileChooser();
            selectedImage = fc.showOpenDialog(null);
            // checking that input file is not null and handling the exception
            if (selectedImage == null)
                return;
            else if (ImageIO.read(selectedImage) == null) {
                Alert alert = new Alert(AlertType.WARNING, "Please upload an image in JPG or PNG format!",
                        ButtonType.OK);
                alert.showAndWait();
                if (alert.getResult() == ButtonType.OK) {
                    return;
                }
            } else {
                Image img = SwingFXUtils.toFXImage(ImageIO.read(selectedImage), null);
                uploadedFilmPoster.setImage(img);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @SuppressWarnings("unchecked")
    @FXML
    public void storeFilmInfo(ActionEvent event) throws ParseException {

        try {
            validateFilmInput();

            // creating JSON objects
            JSONObject films = Main.readJSONFile("films.txt");
            JSONObject filmToAdd = new JSONObject();
            filmToAdd.put("description", filmDescription.getText());
            filmToAdd.put("trailer", filmTrailer.getText());
            filmToAdd.put("startDate", newFilmStartDate.getText());
            filmToAdd.put("endDate", newFilmEndDate.getText());
            filmToAdd.put("time1", newFilmTime1.getText());
            filmToAdd.put("time2", newFilmTime2.getText());
            filmToAdd.put("time3", newFilmTime3.getText());
            filmToAdd.put("age", newFilmAge.getText());
            filmToAdd.put("rating", newFilmRating.getText());
            films.put(filmTitle.getText(), filmToAdd);
            //System.out.println(films.toJSONString());

            // storing film in JSON file - filewriting 
            String path = "C:\\Users\\ruben\\OneDrive\\Documents\\Ruben School\\Year 12\\Computer Science\\IA\\films.txt";
            // System.out.println(path);
            PrintWriter writer = new PrintWriter(new File(path));
            writer.print(films.toJSONString());
            writer.close();

            // storing film poster in film images folder
            String folderPath = "C:\\Users\\ruben\\eclipse-workspace\\Clubhouse Cinemas\\src\\Images";
            File uploads = new File(folderPath);
            File file = new File(uploads, filmTitle.getText() + ".png");
            InputStream input = Files.newInputStream(selectedImage.toPath());
            Files.copy(input, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
            // confirmation alert to inform the employee of the newly added film
            Alert alert = new Alert(AlertType.INFORMATION, "The film " + filmTitle.getText() + " has been added!",
                    ButtonType.OK);
            alert.showAndWait();

            // reloading film list to include the recently added film, and
            // restoring all fields to empty
            // and closing alert on click
            if (alert.getResult() == ButtonType.OK) {
                Main.resetFilmList();
                Main.readJSONFile("films.txt"); //method in main for file reading - adds items to the list
                filmDescription.setText("");
                filmDescription.setText("");
                filmTitle.setText("");
                filmStartDate.setPromptText("dd/MM/yyyy");
                filmEndDate.setPromptText("dd/MM/yyyy");
                filmTime1.setPromptText("hh:mm");
                filmTime2.setPromptText("hh:mm");
                filmTime3.setPromptText("hh:mm");
                filmAge.setPromptText("");
                filmRating.setText("");
                alert.close();
            }
        } catch (FileNotFoundException e) {
            Alert alert = new Alert(AlertType.WARNING, "File Not Found!", ButtonType.OK);
            alert.showAndWait();
            if (alert.getResult() == ButtonType.OK) {
                alert.close();
            }
        } catch (IOException e) {
            Alert alert = new Alert(AlertType.WARNING, "Error: " + e.getMessage(), ButtonType.OK);
            alert.showAndWait();
            if (alert.getResult() == ButtonType.OK) {
                alert.close();
            }
        } catch (InvalidFilmInputException e) {
            Alert alert = new Alert(AlertType.WARNING, e.getMessage(), ButtonType.OK);
            alert.showAndWait();
            if (alert.getResult() == ButtonType.OK) {
                alert.close();
            }
        }
    }
    
    @SuppressWarnings("unlikely-arg-type")
    void validateFilmInput() throws InvalidFilmInputException, ParseException {

        try {
            if (filmTitle.getText().equals("") || filmDescription.getText().equals("")
                    || filmTrailer.getText().equals("") || filmStartDate.getValue().equals("dd/MM/yyyy")
                    || filmEndDate.getValue().equals("dd/MM/yyyy") || filmAge.getValue().equals("") || filmRating.getText().equals(""))
                throw new InvalidFilmInputException("Please complete all fields!");
            else if (selectedImage == null)
                throw new InvalidFilmInputException("Please add the film poster!");
            else if (filmStartDate.getValue().compareTo(LocalDate.now()) < 0)
                throw new InvalidFilmInputException("Start date cannot be before today!");
            else if (filmStartDate.getValue().compareTo(filmEndDate.getValue()) == 0)
                throw new InvalidFilmInputException("Screenings cannot start and end on the same day!");
            else if (filmStartDate.getValue().compareTo(filmEndDate.getValue()) > 0)
                throw new InvalidFilmInputException("End date cannot be before start date!");
            

            // checking that the title of the movie is unique
            for (Film c : Main.getFilmList()) {
                if (c.getTitle().equals(filmTitle.getText()))
                    throw new InvalidFilmInputException(
                            "The title " + filmTitle.getText() + " belongs to another scheduled movie!");
            }

            // looping through the films to find date and time conflicts
            for (Film c : Main.getFilmList()) {

                // converting movie start and end dates to LocalDate for
                // comparison
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
                LocalDate startDateFilms = LocalDate.parse(c.getStartDate(), formatter);
                LocalDate endDateFilms = LocalDate.parse(c.getEndDate(), formatter);

                // if the dates overlap...
                if (!(filmStartDate.getValue().compareTo(endDateFilms) > 0
                        || filmEndDate.getValue().compareTo(startDateFilms) < 0)) {

                    // System.out.println("startDate loop: " + startDateFilms);
                    // System.out.println("endDate loop: " + endDateFilms);

                    // ... and the time(s) overlap as well
                    String[] times = c.getTimes();
                    if (Arrays.asList(times).contains(filmTime1.getValue())
                            || Arrays.asList(times).contains(filmTime2.getValue())
                            || Arrays.asList(times).contains(filmTime3.getValue())) {
                        throw new InvalidFilmInputException("The screening time(s) of your film: " + filmTitle.getText()
                                + " overlap(s) with the film: " + c.getTitle().toString() + "!");
                    }
                }
            }
        } catch (NullPointerException e) {
            throw new InvalidFilmInputException("Please complete all fields!");
        }
    }
}

class InvalidFilmInputException extends Exception {

    private static final long serialVersionUID = 1L;

    InvalidFilmInputException(String s) {
        super(s);
    }
}

以下是它给我的错误:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$KeyHandler.process(Scene.java:4105)
    at javafx.graphics/javafx.scene.Scene.processKeyEvent(Scene.java:2156)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2630)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:218)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:150)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$1(GlassViewEventHandler.java:250)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:249)
    at javafx.graphics/com.sun.glass.ui.View.handleKeyEvent(View.java:548)
    at javafx.graphics/com.sun.glass.ui.View.notifyKey(View.java:972)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.reflect.InvocationTargetException
    at jdk.internal.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
    ... 28 more
Caused by: java.lang.NumberFormatException: empty String
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at java.base/jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
    at java.base/java.lang.Float.parseFloat(Float.java:461)
    at application.FilmManagement.updateFilmText(FilmManagement.java:150)
    ... 38 more
Unexpected token END OF FILE at position 0.
    at org.json.simple.parser.JSONParser.parse(Unknown Source)
    at org.json.simple.parser.JSONParser.parse(Unknown Source)
    at application.Main.readJSONFile(Main.java:83)
    at application.FilmManagement.storeFilmInfo(FilmManagement.java:210)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:832)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8792)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1878)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:557)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:943)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
    ... 46 more
Caused by: java.lang.NullPointerException: Cannot invoke "org.json.simple.JSONObject.put(Object, Object)" because "films" is null
    at application.FilmManagement.storeFilmInfo(FilmManagement.java:221)
    ... 57 more
java json javafx nullpointerexception

评论

1赞 kleopatra 11/13/2021
尽量减少可重复的例子

答:

0赞 Zinedine Benkhider 11/13/2021 #1

你的电影不是空的,这不是问题。

这是你的错误:

Caused by: java.lang.NumberFormatException: empty String.

这意味着您的 JSONParser 尝试将空字符串解析为数字。您需要检查所有条目是否都具有有效的格式。