不可理解的nullpointer异常 - java

时间:2014-03-29 08:18:54

标签: java nullpointerexception

我有一个无法解决的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)

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

请注意:nullpointer并不总是会发生,偶尔会发生一次,但我想把它整理出来。 你们有任何想法吗,我不明白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());

非常感谢,

大卫

2 个答案:

答案 0 :(得分:4)

for (int other : localPlayerList) {

可能的原因是localPlayerList包含Integer null,并且您在自动拆箱到int期间获得了NPE。

答案 1 :(得分:0)

尝试测试&#34; localPlayerList&#34;在&#34; for&#34;之前为空。我不知道&#34; getLocalPlayers()&#34;方法实现,但有可能在不同的时间返回给你不同的结果?该方法是否是线程安全的?