你好 StackOverflowers,
我正在使用(除其他外)Spring 框架进行票务销售计划。我得到了一个非常有趣的 InvocationTargetException,它是由 NullpointerException 引起的。然而,当我检查调试器(IDE IntelliJ)时,抛出 Nullpointer 的那一行,没有任何空值。
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;
}
}
显示标签控制器:
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 对话框(绘制空闲/占用/保留座位的另一个场景)。但首先我检查购物篮中当前是否有所选节目的门票,如果有,我将它们发送到座位表,以便可以根据选择绘制它们。
在 99% 的情况下,这个逻辑运行良好,但现在我们设法创建了 1% 的错误,它突然不再工作了,过程:
在座位表中选择门票进行预订
- 票证被发送到 BasketController
- 在 BasketController 中,票据被发送到数据库并保存
- 保留票从数据库中加载(在不同的选项卡中)
- 其中一些被选中并再次发送到购物篮(以实买)
- 如果我从 BasketElementController 调用更改门票(代表一次购买),SeatingChart 会完美打开(在此期间调用 BasketController.getTicketIdentifiersToShow)
- 但是,如果我使用 ShowTabController.handlePurchase 方法调用更改票证,则会收到以下异常:
例外
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$38/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 中的所有内容时,没有一个变量为空,showsMap 具有不应引发 NullpointerException 的有效键和有效值。
编辑:在调试器中显示它们是!= null,但是 LogStatement 显示,即使显示 Map.get(showDto) == null,即使 showMap.values() != null 并且只有一个键。
这可能是由控制器类上的 DependencyInjection 引起的吗?注入的对象可以突然变为空?一些帮助和提示将不胜感激。
PS:我尽力保持代码样本尽可能小,同时仍然显示相关的地方。