2014-03-29 4 views
1

nullpointer 예외가 있습니다. 해결할 수없는 예외가 있습니다. 여러분이보기에 좋을 것 같습니다. PlayerUpdateTask의 84알 수없는 nullpointer 예외 - Java

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) 

라인 : 다음 nullpointer하지 않는 :

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 

주의 사항이

for (int other : localPlayerList) { 

코드 최대 이것은 내가 받고있어 예외 항상 일어난다, 그것은 때때로 일어난다. 그러나 나는 그것을 소트하고 싶다. 너희들은 어떤 생각을 가지고 있니, 나는 localPlayerList가 null 일 수 있다는 것을 이해하지 못한다. 이것은 자바 게임을위한 것입니다. 여기

로컬 플레이어 목록이 채워집니다 방법은 다음과 같습니다 가능성이 이유는 localPlayerListnullInteger 포함되어 있다는 것입니다 많이

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

감사합니다,

데이비드

+0

정확하게 무엇이 진행되고 있는지를 알려면'player.getLocalPlayers()'리스트가 어떻게 채워지는지 정말로 알아야합니다. 그러나 분명히 말했듯이, 그 안에는 null이 있습니다. – Radiodef

+0

추가했습니다. 고맙습니다. – user3475308

답변

4
for (int other : localPlayerList) { 

하고 자동 언 박싱 중 NPE가 int으로 변경되었습니다.

+0

고마워요. 이제이 목록에 이런 식으로 추가하고 있습니다. \t 목록 localPlayerList = new ArrayList (); 위한 \t \t \t \t \t \t (정수 I : player.getLocalPlayers()) { \t \t \t \t 경우 (! I = NULL) { \t \t \t \t \t localPlayerList.add (I); \t \t \t \t} \t \t \t} – user3475308

+1

@ 문제는,이다'player.getLocalPlayers입니다 user3475308()'널 (null)를 포함 여부를하는데? 그렇지 않다면 문제를 숨기지 말고 null을 무시하십시오. – Boann

0

"for"앞에 "localPlayerList"가 null인지 테스트 해보십시오. "getLocalPlayers()"메소드 구현을 모르지만 다른 시간에 다른 결과를 반환 할 수 있습니까? 그 방법은 스레드로부터 안전합니까?

+0

내 IDE에서이 시점에서 개체가 null이 될 수 없다고 알려 주시면 고맙습니다. 이 사람들이 이전에 말했듯이, localPlayerList 내부에있는 것이 분명합니다. – user3475308