У меня есть исключение nullpointer, которое я, похоже, не могу решить, я бы хотел, чтобы вы, ребята, посмотрели. Это исключение, которое я получаю:
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)
Строка 84 PlayerUpdateTask:
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
Обратите внимание: null-указатель не всегда случается, это случается раз в то время, но я бы хотел разобраться. Вы, ребята, знаете, я не понимаю, как 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 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
, и вы получаете NPE во время автоматического распаковки до int
.
player.getLocalPlayers()
содержать нули или нет? Если нет, то исправьте это, не скрывайте проблему, игнорируя нули.
Попробуйте проверить, если "localPlayerList" имеет значение null перед "for". Я не знаю реализацию метода getLocalPlayers(), но возможно ли вернуть вам разные результаты в разное время? Является ли этот метод потокобезопасным?
player.getLocalPlayers()
чтобы сказать, что именно происходит. Но, как сказано, ясно, что в нем нет нуля.