JavaFX NoSuchElementException

时间:2018-08-30 16:21:50

标签: java javafx

我想使用菜单按钮重新启动我的Klondike Java应用程序,但是编译器总是使用以下文本引发错误: “线程“ JavaFX应用程序线程”中的异常java.util.NoSuchElementException”

代码是:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.application.Platform;
import javafx.scene.control.SeparatorMenuItem;

public class Klondike extends Application {

private static final double WINDOW_WIDTH = 1400;
private static final double WINDOW_HEIGHT = 900;


public static void main(String[] args) {
    launch(args);
}


public void startGame(Stage primaryStage) {
    Card.loadCardImages();

    Game game = new Game();
    BorderPane root = new BorderPane(game);
    Scene scene =  new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
    scene.setRoot(root);
    game.setTableBackground(new Image("/table/green.png"));

    MenuBar menuBar = new MenuBar();
    menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
    root.setTop(menuBar);

    Menu fileMenu = new Menu("File");
    MenuItem newMenuItem = new MenuItem("New Game");
    MenuItem exitMenuItem = new MenuItem("Exit");
    exitMenuItem.setOnAction(ActionEvent -> Platform.exit());
    newMenuItem.setOnAction( e ->
    {
        System.out.println( "Restarting app!" );
        primaryStage.close();
        start(primaryStage);

    } );

    fileMenu.getItems().addAll(newMenuItem, new SeparatorMenuItem(), exitMenuItem);

    menuBar.getMenus().addAll(fileMenu);


    primaryStage.setTitle("Klondike Solitaire");
    primaryStage.setScene(scene);
    primaryStage.show();

}

@Override
public void start(Stage primaryStage) {
    startGame(primaryStage);
}

}

这是一个学校项目。

堆栈跟踪:

Exception in thread "JavaFX Application Thread" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(ArrayList.java:862)
at com.codecool.klondike.Game.dealCards(Game.java:278)
at com.codecool.klondike.Game.<init>(Game.java:155)
at com.codecool.klondike.Klondike.startGame(Klondike.java:29)
at com.codecool.klondike.Klondike.start(Klondike.java:65)
at com.codecool.klondike.Klondike.lambda$startGame$1(Klondike.java:47)
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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
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)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

Game类中的dealCards方法:

public void dealCards() {
    Iterator<Card> deckIterator = deck.iterator();
    int cardsToAdd = 1;
    for(Pile tableauPile: tableauPiles){  
        for (int i = 0; i < cardsToAdd; i++) {
            Card cardToAdd = deckIterator.next(); //This is line 278
            if (i + 1  == cardsToAdd){
                cardToAdd.flip();
            }
            tableauPile.addCard(cardToAdd);
            addMouseEventHandlers(cardToAdd);
            getChildren().add(cardToAdd);
        }
        cardsToAdd++;
    }
    deckIterator.forEachRemaining(card -> {
        stockPile.addCard(card);
        addMouseEventHandlers(card);
        getChildren().add(card);
    });

}

0 个答案:

没有答案