这是在必须从游戏中移除玩家时调用的方法。这两种方法都属于不同的类别。
GameboardGUI类
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();
}
}
调用该方法的方法如下:
如果当前玩家(来自我)登陆房产并且无法支付租金(即由于takefrombalance(1200);
他们的余额达到0,目前硬编码为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)
答案 0 :(得分:1)
如果没有堆栈跟踪和代码的行号,真的很难帮助你,但我们正在尝试。
如果那是真的那条线(实际上不在removePlayerFromGame
内),则成员gui
尚未初始化。
当然,如果它不是那条线,那么它可能就是playerToRemove
为空,而NullPointerException实际上发生在上面几行。
或者,另一种可能性是它发生在removePlayerFromGame
内,在这种情况下,最可能的情况是playerToRemove
尚未正确设置 - 其getPropertiesOwned()
或{{ 1}}方法返回null。
答案 1 :(得分: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正在做一些事情,但我认为情况并非如此,因为stacktrace将包含其他方法。
答案 2 :(得分:0)
你在代码中说到达到行
时抛出了异常gui.removePlayerFromGame(fromMe);
但是,您显示的堆栈跟踪的顶部是Player.movePlayer(),其底部位于PropertyTile.payRent()。
这个payRent()接收任何参数吗?你可以发布消息来源吗?