简体   繁体   English

无法理解的nullpointer异常-Java

[英]Not understandable nullpointer exception - java

I have a nullpointer exception I cannot seem to solve, I'd love for you guys to take a look. 我有一个似乎无法解决的nullpointer异常,我希望你们看看。 This is the exception I'm receiving: 这是我收到的异常:

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)

Line 84 of PlayerUpdateTask is: PlayerUpdateTask的第84行是:

for (int other : localPlayerList) {

The code up to there is 到目前为止的代码是

    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

Please note: the nullpointer does not always happen, it happens once in a while but I'd like to sort it out. 请注意:nullpointer并不总是发生,它偶尔会发生一次,但我想对它进行梳理。 Do you guys have any idea, I do not understand how localPlayerList can be null considering I initialize it earlier in the method. 你们有什么主意吗,考虑到我在方法中较早地初始化了它,所以我不明白localPlayerList如何可以为null。 This is for a java game, by the way. 顺便说一下,这是一个Java游戏。

Here's how the local player list is populated: 以下是本地玩家列表的填充方式:

    //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());

Thanks alot, 非常感谢,

David 大卫

for (int other : localPlayerList) {

可能的原因是localPlayerList包含一个为nullInteger ,并且在自动将localPlayerList拆箱到int期间,您正在获得NPE。

Try to test if "localPlayerList" is null just before the "for". 尝试测试“ local”是否在“ for”之前为null。 I don't know "getLocalPlayers()" method implementation, but is possible that return to you different results in different times? 我不知道“ getLocalPlayers()”方法的实现,但是有可能在不同的时间返回不同的结果吗? Is that method thread-safe? 该方法是线程安全的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM