StackOverflowers さん、こんにちは。
私は (特に) Spring Framework を使用してチケット販売プログラムに取り組んでいます。NullpointerException が原因で、非常に興味深い InvocationTargetException が発生しています。ただし、デバッガー (IDE IntelliJ) で確認したところ、Nullpointer がスローされた行に null はありません。
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 ダイアログを呼び出します (自由/占有/予約済みの座席が描画される別のシーン)。しかし、最初に、現在ショッピング バスケットに選択したショーのチケットがあるかどうかを確認し、ある場合はそれらを SeatingChart に送信して、選択したとおりに描画できるようにします。
99% では、このロジックは完全にうまく機能しますが、1% のバグが突然機能しなくなるプロセスを作成することができました。
チケットは座席表で予約のために選択されます
- チケットは BasketController に送信されます
- In BasketController チケットはデータベースに送信され、保存されます
- 予約済みチケットはデータベースからロードされます (別のタブで)
- これらのいくつかは選択され、ショッピング バスケットに再度送信されます (実際に購入するため)。
- BasketElementController (1 つの購入を表す) からチケットの変更を呼び出すと、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 内のすべてをチェックすると、どの変数も null ではなく、showsMap には NullpointerException をスローしない有効なキーと有効な値があります。
編集:デバッガーでは != null であることが示されていますが、LogStatement は、showsMap.get(showDto) == null であることを示しています。
これは、Controller クラスの DependencyInjection によって何らかの形で発生する可能性がありますか? 注入されたオブジェクトが突然 null になることはありますか? いくつかの助けとヒントをいただければ幸いです。
PS: 関連する場所を表示しながら、コード サンプルをできるだけ小さくするように最善を尽くしました。