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

不可理解的空指针异常-java,java,nullpointerexception,Java,Nullpointerexception,我有一个空指针异常,我似乎无法解决,我想让你们看看。 这是我收到的例外情况: 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

我有一个空指针异常,我似乎无法解决,我想让你们看看。 这是我收到的例外情况:

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
包含一个
Integer
,即
null
,并且在自动取消绑定到
int
的过程中,您会得到一个NPE


可能的原因是
localPlayerList
包含一个
Integer
,即
null
,在自动取消绑定到
int

的过程中,您将获得一个NPE,尝试测试“localPlayerList”是否在“for”之前为null。我不知道“getLocalPlayers()”方法的实现,但在不同的时间返回不同的结果是否可能?这个方法是线程安全的吗

尝试测试“localPlayerList”是否在“for”之前为null。我不知道“getLocalPlayers()”方法的实现,但在不同的时间返回不同的结果是否可能?这个方法是线程安全的吗

我们真的需要看看
player.getLocalPlayers()
列表是如何填充的,以告诉我们到底发生了什么。但是正如前面所说的,很明显它里面有一个空值。我已经添加了它,谢谢。我们真的需要看看
player.getLocalPlayers()
列表是如何填充的,以说明到底发生了什么。但如前所述,很明显它里面有一个空值。我已经添加了它,谢谢。谢谢你的评论,如果我尝试这样做的话,我的IDE告诉我对象此时不能为空。因此,正如这些人之前所说,这肯定是LocalPlayerList内部的一些东西。谢谢您的评论,如果我尝试这样做,我的IDE告诉我,此时对象不能为null。所以,正如这些人之前所说,这肯定是本地玩家列表中的一些东西。谢谢,我现在以这种方式将其添加到列表中,这可能吗?List localPlayerList=new ArrayList();对于(整数i:player.getLocalPlayers()){if(i!=null){localPlayerList.add(i);}}@user3475308,问题是,
player.getLocalPlayers()
是否应该包含null?如果没有,那么请解决这个问题,不要通过忽略空值来隐藏问题。谢谢,我现在以这种方式添加到列表中,这可能吗?List localPlayerList=new ArrayList();对于(整数i:player.getLocalPlayers()){if(i!=null){localPlayerList.add(i);}}@user3475308,问题是,
player.getLocalPlayers()
是否应该包含null?如果没有,那么请解决这个问题,不要通过忽略空值来隐藏问题。
    //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) {