SpringFramework 项目中 FXMLLoader 中的 InvocationTargetException

InvocationTargetException in FXMLLoader in SpringFramework Project

你好 Whosebugers,

我正在使用(以及其他)Spring 框架开发门票销售程序。我收到一个非常有趣的 InvocationTargetException,它是由 NullpointerException 引起的。但是,当我使用调试器 (IDE IntelliJ) 检查时,抛出空指针的那一行没有空值。

BasketController(购物篮控制器):

package ticketline.client.gui.controller;

import ticketline.client.util.SpringFxmlLoader;
import ticketline.dto.*;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URL;
import java.util.*;

/**
 * Created by Sebastian on 11.05.2015.
 */
@Component
public class BasketController implements Initializable {

    private static final Logger LOGGER = LoggerFactory.getLogger(BasketController.class);

    @Autowired
    private SpringFxmlLoader springFxmlLoader;

    private HashMap<ShowDto, List<ReceiptEntryDto>> showsMap;

    private List<ReceiptEntryDto> receiptEntryDtos;

    /**
     * {@inheritDoc}
     */
    @Override
    public void initialize(URL url, ResourceBundle resBundle) {
        initNewBasket();
    }

    /**
     * Resets all fields, call if process was cancelled and after basket elements were purchased.
     */
     private void initNewBasket() {
        receiptEntryDtos = new ArrayList<>();
        showsMap = new HashMap<>();
     }

    /**
     * Checks if there is a version of this ShowDto in the ShoppingBasket and gives back that element if present,
     * else returns the parameter
     *
     * @param showDto
     * @return the parameter if not in basket, else the version with lists of chosen tickets
     */
    public Boolean showInBasket(ShowDto showDto) {

        for (ShowDto s : showsMap.keySet()) {
            if (s.getId().equals(showDto.getId())) {
                return true;
            }
        }
        return false;
    }

    /**
     * Gets all TicketIdentiferDtos in Basket of a certain Show
     *
     * @param showDto of which to get all TicketIdentifiers
     * @return list of all TicketIdentifiers
     */
    public List<TicketIdentifierDto> getTicketIdentifiersToShow(ShowDto showDto) {
        List<TicketIdentifierDto> toReturn = new ArrayList<>();

        // Next Line throws NullPointerException, no Objects are null though
        for (ReceiptEntryDto r : showsMap.get(showDto)) {
            toReturn.add(r.getTicketIdentifier());
        }
        return toReturn;
    }

    /**
     * Gets the Show to the specified Ticket
     *
     * @param ticket ticket for which to get the Show
     * @return the ShowDto of the ticket
     */
    private ShowDto getShowByTicket(TicketDto ticket) {

        for (ShowDto show : showsMap.keySet()) {
            for (ReceiptEntryDto re : showsMap.get(show)) {
                if (re.getTicketIdentifier().getTicket().getId().equals(ticket.getId())) {
                    return show;
                }
            }
        }
        return null;
    }
}

ShowTabController:

package ticketline.client.gui.controller;

import ticketline.client.util.SpringFxmlLoader;
import ticketline.dto.ShowDto;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URL;
import java.util.Date;
import java.util.List;

/**
 * Created by Sebastian on 08.05.2015.
 */
@Component
public class ShowTabController implements Initializable {

    private static final Logger LOGGER = LoggerFactory.getLogger(ShowTabController.class);

    private ObservableList<ShowDto> showData = FXCollections.observableArrayList();

    @Autowired
    private SpringFxmlLoader springFxmlLoader;

    @Autowired
    private SeatingChartController seatingChartController;

    @Autowired
    private BasketController basketController;    

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    /**
     * Handle the event in which the Buy / Reserve Button was pressed
     */
    @FXML
    private void handlePurchase(MouseEvent event) {

        if (event.getClickCount() == 2) {

            if (tableShows.getSelectionModel().getSelectedIndex() >= 0) {
                SpringFxmlLoader.LoadWrapper wrapper = springFxmlLoader.loadAndWrap("/gui/fxml/seatingChartDialog.fxml");
                ShowDto selected = tableShows.getSelectionModel().getSelectedItem();

                Stage seatChartStage = new Stage();
                seatChartStage.setScene(new Scene((Parent) wrapper.getLoadedObject()));
                seatChartStage.setResizable(false);
                seatChartStage.initModality(Modality.APPLICATION_MODAL);
                seatChartStage.initOwner(tableShows.getScene().getWindow());
                seatChartStage.setTitle(BundleManager.getBundle().getString("chart.title"));
                seatChartStage.getIcons().add(new Image(LoginController.class.getResourceAsStream("/image/ticketlineLogo.png")));

                if (basketController.showInBasket(selected)) {

                    // This Call brings us to the basketController
                    seatingChartController.setupSeatingChart(selected, basketController.getTicketIdentifiersToShow(selected));
                } else {
                    seatingChartController.setupSeatingChart(selected, null);
        }
    }
}

在 ShowTabController 中有一个包含 ShowDtos 的 TableView,在 handlePurchase 方法中我调用了 SeatingChart 对话框(另一个场景 free/taken/reserved 绘制了座位)。但首先我检查购物篮中当前是否有任何所选演出的门票,如果有,我将它们发送到 SeatingChart,以便根据选择绘制它们。

在 99% 的情况下,这个逻辑工作得很好,但现在我们设法创建了 1% 的 Bug,它突然不再工作了,过程:

在座位表中选择要预订的门票

异常

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    [..more lines..]
    at com.sun.glass.ui.win.WinApplication$$Lambda/1786955566.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: 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:497)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
    ... 35 more
Caused by: java.lang.NullPointerException
    at ticketline.client.gui.controller.BasketController.getTicketIdentifiersToShow(BasketController.java:338)
    at ticketline.client.gui.controller.ShowTabController.handlePurchase(ShowTabController.java:252)
    ... 45 more

但是,当我使用调试器检查 BasketController 中的所有内容时,none 变量为空,showsMap 具有不应抛出 NullpointerException 的有效键和有效值。

编辑:显示它们在调试器中是 != null,但是 LogStatement 显示 showsMap.get(showDto) == null,即使 showsMap.values() != null 并且那里只有一把钥匙。

这在某种程度上可能是由控制器上的 DependencyInjection 引起的 类?一个注入的对象可以突然变为空? 非常感谢一些帮助和提示。

PS:我尽最大努力使代码示例尽可能小,同时仍显示相关地点。

大家应该明白HashMap中的Hash其实是有含义的。似乎 ShowDto 是真正不同的对象,因为一个是从数据库中再次加载的,因此散列不相同,导致 NullpointerException。 更简单的解决方案是简单地在 ShowDto.

中实现 Object.hashCode 方法