Hello StackOverflowers,
我正在使用(以及其他)Spring Framework开发Ticket Sales Program。我得到一个非常有趣的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;
}
}
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 / take / reserved Seats)。但首先,我检查目前购物篮中所选择的Show是否有任何门票,如果我将它们发送到SeatingChart,那么它们可以按照所选择的方式绘制。
99%的这种逻辑非常有效,但现在我们设法创造了1%的Bug,它突然不再起作用了,进程:
为SeatingChart中的预订选择门票
异常
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中的所有内容时,没有变量为null,showsMap具有有效的Key和有效值,不应该抛出NullpointerException。
编辑:在调试器中显示它们是!= null,然而,LogStatement显示,showMap.get(showDto)== null,即使showsMap.values()!= null并且只有一个键。< / p>
这可能在某种程度上是由Controller类的DependencyInjection引起的吗?注入的对象可以突然变为空? 一些帮助和提示将不胜感激。
PS:我尽力保持代码样本尽可能小,同时仍显示相关位置。
答案 0 :(得分:0)
应该意识到HashMap中的Hash实际上意味着什么。看起来ShowDto实际上是不同的对象,因为一个从数据库再次加载,因此散列不一样导致NullpointerException。 更简单的解决方案是在ShowDto中简单地实现Object.hashCode方法。