Java NullPointerException我无法解析
这是在必须将玩家移出游戏时调用的方法。这两种方法属于不同的类 游戏板GUI类Java NullPointerException我无法解析,java,nullpointerexception,Java,Nullpointerexception,这是在必须将玩家移出游戏时调用的方法。这两种方法属于不同的类 游戏板GUI类 Vector<Player> players = new Vector<Player>(); public void removePlayerFromGame(Player playerToRemove) { //go through the playerToRemove's properties and reset all their
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();
}
}
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)
如果没有stacktrace和代码行号,真的很难帮助您,但我们正在尝试 如果这确实是行(实际上不在
removePlayerFromGame
中),则成员gui
尚未初始化
当然,如果不是那一行,很可能是playerToRemove
为null,而NullPointerException实际上发生在上面几行
或者,另一种可能性是它发生在
removePlayerFromGame
中,在这种情况下,最有可能的情况是playerToRemove
未正确设置-其getPropertiesOwned()
或getTransportsOwned()
方法返回null。如果没有堆栈跟踪和代码行号,很难帮助您,但我们正在尝试
如果这确实是行(实际上不在removePlayerFromGame
中),则成员gui
尚未初始化
当然,如果不是那一行,很可能是playerToRemove
为null,而NullPointerException实际上发生在上面几行
或者,另一种可能性是它发生在
removePlayerFromGame
中,在这种情况下,最有可能的情况是playerToRemove
没有正确设置-它的getPropertiesOwned()
或getTransportsOwned()
方法返回空值,作为了解情况的第一步:
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将包含其他方法。我将添加以下两行,作为第一步,看看会发生什么:
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将包含其他方法。您在代码中说,当您到达该行时会抛出异常
gui.removePlayerFromGame(fromMe);
但是,您显示的stacktrace的顶部是Player.movePlayer(),底部是PropertyTile.payRent()
此payRent()是否接收任何参数?您可以发布它的源代码吗?您在代码中说,当您到达该行时,会引发异常
gui.removePlayerFromGame(fromMe);
但是,您显示的stacktrace的顶部是Player.movePlayer(),底部是PropertyTile.payRent()
此payRent()是否接收任何参数?你能发布它的源代码吗?基本上是方法调用gui.removePlayerFromGame(fromMe);就是将玩家“从我身边”移出游戏,因为他们付不起租金。它为该行抛出空指针异常。我不知道为什么。这是唯一与该方法相关的代码。您是否尝试过调试?gui定义在哪里?@IEatSandvich Stack Trace?而
gui
在哪里?我在Class
classI的任何地方都看不到它,我看不到gui在任何地方被实例化。属性块中的254行是什么?基本上是方法调用gui.removePlayerFromGame(fromMe);就是将玩家“从我身边”移出游戏,因为他们付不起租金。它为该行抛出空指针异常。我不知道为什么。这是唯一与该方法相关的代码。您是否尝试过调试?gui定义在哪里?@IEatSandvich Stack Trace?而gui
在哪里?我在Class
classI的任何地方都看不到它,我看不到gui在任何地方被实例化。属性块中的254行是什么?它已经被实例化了。在类的顶部,它被初始化为GameboardGUI=new GameboardGUI();(抱歉,我没有显示这一点)和GameboardGUI类中的玩家是向量玩家代码>我让一些人仔细阅读了代码,没有人能看出代码在语法上有任何错误。我有点希望有人能从经验中知道问题可能是什么。@IEatSandvich请用这些信息更新帖子,你能发布堆栈跟踪吗?以前从未做过堆栈跟踪,但通过谷歌搜索,这只是一个输入行new Exception().printStackTrace()的问题吗代码>?如果是的话,你建议我把它放在代码中的什么地方?它已经被删除了。在类的顶部,它被初始化为GameboardGUI=new GameboardGUI();(抱歉,我没有显示这一点)和GameboardGUI类中的玩家是向量玩家代码>我让一些人仔细阅读了代码,没有人能看出代码在语法上有任何错误。我有点希望有人能从经验中知道问题可能是什么。@IEatSandvich请用这些信息更新帖子,你能发布堆栈跟踪吗?以前从未做过堆栈跟踪,但通过谷歌搜索,这只是一个输入行new Exception().printStackTrace()的问题吗代码>?如果是这样,您建议我将其放在代码中的什么位置?您是每次还是有时都会收到堆栈跟踪/错误?如果你点击一个按钮两次,你会得到它吗?继续得到它。我以前也有过类似的情况,如果我删除代码并重新键入,它最终会正常工作。可能会完全重写它。做得有点不同。看看它是否会以不同的方式工作。谢谢大家。您是每次还是有时都会收到堆栈跟踪/错误?如果你点击一个按钮两次,你会得到它吗?继续得到它。我以前也有过类似的情况,如果我删除代码并重新键入,它最终会正常工作。可能会完全重写它。做得有点不同。看看是不是