gpt4 book ai didi

java - 不可理解的空指针异常 - java

转载 作者:行者123 更新时间:2023-11-29 03:22:42 26 4
gpt4 key购买 nike

我有一个似乎无法解决的空指针异常,我希望你们能看一看。这是我收到的异常:

java.lang.NullPointerException
at org.ikov.engine.task.impl.PlayerUpdateTask.execute(PlayerUpdateTask.java:84)
at org.ikov.engine.task.ParallelTask$1.run(ParallelTask.java:44)
at org.ikov.engine.GameEngine$4.run(GameEngine.java:160)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

PlayerUpdateTask 的第 84 行是:

for (int other : localPlayerList) {

到这里的代码是

    if(player.getLocalPlayers() == null) {
player.disconnected = true;
return;
}
List<Integer> localPlayerList = new ArrayList<Integer>(player.getLocalPlayers());

/*
* If the map region changed send the new one. We do this immediately as
* the client can begin loading it before the actual packet is received.
*/
if (player.mapRegionDidChange) {
player.getActionSender().sendMapRegion();
}

/*
* The update block packet holds update blocks and is send after the
* main packet.
*/
GamePacketBuilder updateBlock = new GamePacketBuilder();

/*
* The main packet is written in bits instead of bytes and holds
* information about the local list, players to add and remove, movement
* and which updates are required.
*/
GamePacketBuilder packet = new GamePacketBuilder(81,
GamePacket.Type.VARIABLE_SHORT);
packet.startBitAccess();

/*
* Updates this player.
*/
updateThisPlayerMovement(packet);
updatePlayer(updateBlock, player, false, true);

/*
* Write the current size of the player list.
*/
packet.putBits(8, localPlayerList.size());

//Set up a deletion queue
List<Integer> deletionQueue = new ArrayList<Integer>();

/*
* Iterate through the local player list.
*/ - FROM HERE THE NULLPOINTER starts

请注意:空指针并不总是发生,它偶尔会发生一次,但我想解决它。你们有什么想法吗,我不明白 localPlayerList 怎么会是 null 考虑到我在方法的前面初始化了它。顺便说一下,这是一个 java 游戏。

本地玩家列表的填充方式如下:

    //We keep track of the amount of players we've added, we want to keep it down a bit as we don't want to loverload people's client
int addedPlayers = 0;

/*
* Loop through every player.
*/
for (Player otherPlayer : PlayerManager.getSingleton().getPlayers()) {

if (otherPlayer == null) {
continue;
}
if (!player.activatedPlayerUpdate) {
break;
}
if (!player.withinDistance(otherPlayer)) {
/*
* Check that the Player is within good distance of the player
* before adding to local list.
*/
continue;
}

/*
* Check if there is room left in the local list.
*/
if (player.getLocalPlayers().size() >= 255 || addedPlayers >= 20) {
/*
* There is no more room left in the local list. We cannot add
* more players, so we just ignore the extra ones. They will be
* added as other players get removed.
*/
break;
}

/*
* Do not add anymore data to the packet if it the packet exceeds
* the maximum packet size as this will cause the client to crash.
*/
if (packet.getLength() + updateBlock.getLength() >= 3072) {
break;
}

/*
* If they should not be added ignore them.
*/
if (otherPlayer == player
|| player.getLocalPlayers()
.contains(otherPlayer.getIndex())
|| !otherPlayer.isVisible()
|| otherPlayer.getMapInstance() != player.getMapInstance()) {
continue;
}

/*
* Add the player to the local list if it is within distance.
*/
player.getLocalPlayers().add(otherPlayer.getIndex());
addedPlayers++;

/*
* Add the player in the packet.
*/
addNewPlayer(packet, otherPlayer);

/*
* Update the player, forcing the appearance flag.
*/
updatePlayer(updateBlock, otherPlayer, true, false);
}

/*
* Check if the update block is not empty.
*/
if (!updateBlock.isEmpty()) {
/*
* Write a magic id indicating an update block follows.
*/
packet.putBits(11, 2047);
packet.finishBitAccess();

/*
* Add the update block at the end of this packet.
*/
packet.put(updateBlock.toPacket().getPayload());
} else {
/*
* Terminate the packet normally.
*/
packet.finishBitAccess();
}

/*
* Write the packet.
*/
player.write(packet.toPacket());

非常感谢,

大卫

最佳答案

for (int other : localPlayerList) {

可能的原因是 localPlayerList 包含一个 Integer,它是 null,并且您在自动拆箱到 int.

关于java - 不可理解的空指针异常 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22728725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com