1

这是当玩家必须从游戏中移除时调用的方法。这两种方法都属于不同的类。

游戏板GUI类

    Vector<Player> players = new Vector<Player>();

    public void removePlayerFromGame(Player playerToRemove)
        {
            //go through the playerToRemove's properties and reset all their variables
            for(int i = 0; i < playerToRemove.getPropertiesOwned().size(); i++)
            {
                //code to reset the player's properties to an unowned/unmodified state
            }
            //same with transports
            for(int i = 0; i < playerToRemove.getTransportsOwned().size(); i++)
            {
                //code to reset the player's transports to an unowned/unmodified state
            }
            //just updating the vector based on the playerToRemove's position
            if(players.get(0) == playerToRemove)
            {
                players.remove(playerToRemove);
                updatePlayerInformation();
            }
            else 
            {
                players.remove(playerToRemove);
                updatePlayerVector(players);
                updatePlayerInformation();
            }

        }

这就是该方法的调用方式:如果当前玩家 (fromMe) 登陆一个房产并且无法支付租金(即他们的余额由于 而达到 0 takefrombalance(1200);,目前硬编码为 1200 以使测试更容易),他们将被移除if 语句if(fromMe.isBankrupt())

属性类

GameboardGUI gui = new GameboardGUI();

public void payRent(Player fromMe, Player toYou, int rent)
    {
        //remove rent from the current player
        fromMe.takeFromBalance(1200);
        //add it to the owner
        toYou.addToBalance(rent);
        GameboardGUI.addGameFeedMessage(fromMe.getName() + " has paid " + rent + " in rent to " + toYou.getName());
        GameboardGUI.addGameFeedMessage(toYou.getName() + "'s balance is now " + toYou.getBalance());
        if(fromMe.isBankrupt())
        {
            //THIS IS THE CALL THAT THROWS THE NullPointerException
            gui.removePlayerFromGame(fromMe);
        }
        else 
        {
            GameboardGUI.addGameFeedMessage(fromMe.getName() + "'s balance is now " + fromMe.getBalance());
        }
    }

这是到达该行时的堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at PropertyTile.payRent(PropertyTile.java:254)
    at PropertyTile.landedOnProperty(PropertyTile.java:239)
    at GameboardGUI.playerHasLanded(GameboardGUI.java:1905)
    at Player.setPosition(Player.java:82)
    at Player.movePlayer(Player.java:101)
    at GameboardGUI$11.actionPerformed(GameboardGUI.java:1536)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
4

3 回答 3

1

如果没有堆栈跟踪和代码行号,真的很难为您提供帮助,但我们正在努力。

如果这确实是行(而不是实际上在 内removePlayerFromGame),则该成员gui尚未初始化。

当然,如果不是那一行,它可能只是那playerToRemove是空的,并且 NullPointerException 实际上发生在上面几行。

或者,另一种可能性是它发生在 内removePlayerFromGame,在这种情况下,最有可能的情况是playerToRemove没有正确设置——它的getPropertiesOwned()getTransportsOwned()方法返回 null。

于 2013-04-19T19:46:33.173 回答
0

我将添加以下两行,作为第一步,看看会发生什么:

if(fromMe==null)
System.out.println("from me is null");

if(gui == null)
System.out.println("gui is null"); 

gui.removePlayerFromGame(fromMe);

两个对象引用之一是 null 导致异常。否则,方法 removePlayerFromGame 正在做某事,但我认为不会是这种情况,因为堆栈跟踪将包含其他方法。

于 2013-04-19T20:48:38.603 回答
0

您在代码中说当您到达该行时抛出异常

gui.removePlayerFromGame(fromMe);

但是,您显示的堆栈跟踪的顶部是 Player.movePlayer(),其底部位于 PropertyTile.payRent()。

这个 payRent() 是否接收任何参数?可以发一下源码吗?

于 2013-04-19T21:51:17.223 回答