2012-02-14 4 views
-1

スレッド "main"の例外java.lang.NumberFormatException:入力文字列: "npcId" 実行しようとすると。私のコードで何が問題になっていますか?スレッド "main"の例外java.lang.NumberFormatException:入力文字列: "npcId"

どうしたのですか? クラスがしていることは、NPCのアン​​パックされた定義を読み込み、パックすることですが、正しくパックされていません。

クラス(フォーマットのために私が使用している下記参照):

package com.rs.utils; 

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 
import java.util.HashMap; 

import com.rs.game.npc.combat.NPCCombatDefinitions; 

// Referenced classes of package com.rs.utils: 
//   Logger 

public final class NPCCombatDefinitionsL 
{ 

    public static void init() 
    { 
     if((new File("data/npcs/packedCombatDefinitions.ncd")).exists()) 
      loadPackedNPCCombatDefinitions(); 
     else 
      loadUnpackedNPCCombatDefinitions(); 
    } 

    public static NPCCombatDefinitions getNPCCombatDefinitions(int npcId) 
    { 
     NPCCombatDefinitions def = (NPCCombatDefinitions)npcCombatDefinitions.get(Integer.valueOf(npcId)); 
     if(def == null) 
      return DEFAULT_DEFINITION; 
     else 
      return def; 
    } 

    private static void loadUnpackedNPCCombatDefinitions() 
    { 
     int count = 0; 
     Logger.log("NPCCombatDefinitionsL", "Packing npc combat definitions..."); 
     try 
     { 
      DataOutputStream out = new DataOutputStream(new FileOutputStream("data/npcs/packedCombatDefinitions.ncd")); 
      BufferedReader in = new BufferedReader(new FileReader("data/npcs/unpackedCombatDefinitionsList.txt")); 
      do 
      { 
       String line = in.readLine(); 
       count++; 
       if(line == null) 
        break; 
       if(!line.startsWith("//")) 
       { 
        String splitedLine[] = line.split(" - ", 2); 
        if(splitedLine.length != 2) 
         throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(count).append(", ").append(line).toString()); 
        int npcId = Integer.parseInt(splitedLine[0]); 
        String splitedLine2[] = splitedLine[1].split(" ", 12); 
        if(splitedLine2.length != 12) 
         throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(count).append(", ").append(line).toString()); 
        int hitpoints = Integer.parseInt(splitedLine2[0]); 
        int attackAnim = Integer.parseInt(splitedLine2[1]); 
        int defenceAnim = Integer.parseInt(splitedLine2[2]); 
        int deathAnim = Integer.parseInt(splitedLine2[3]); 
        int attackDelay = Integer.parseInt(splitedLine2[4]); 
        int deathDelay = Integer.parseInt(splitedLine2[5]); 
        int respawnDelay = Integer.parseInt(splitedLine2[6]); 
        int maxHit = Integer.parseInt(splitedLine2[7]); 
        int attackStyle; 
        if(splitedLine2[8].equalsIgnoreCase("MELEE")) 
         attackStyle = 0; 
        else 
        if(splitedLine2[8].equalsIgnoreCase("RANGE")) 
         attackStyle = 1; 
        else 
        if(splitedLine2[8].equalsIgnoreCase("MAGE")) 
         attackStyle = 2; 
        else 
        if(splitedLine2[8].equalsIgnoreCase("SPECIAL")) 
         attackStyle = 3; 
        else 
        if(splitedLine2[8].equalsIgnoreCase("SPECIAL2")) 
         attackStyle = 4; 
        else 
         throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(line).toString()); 
        int attackGfx = Integer.parseInt(splitedLine2[9]); 
        int attackProjectile = Integer.parseInt(splitedLine2[10]); 
        int agressivenessType; 
        if(splitedLine2[11].equalsIgnoreCase("PASSIVE")) 
         agressivenessType = 0; 
        else 
        if(splitedLine2[11].equalsIgnoreCase("AGRESSIVE")) 
         agressivenessType = 1; 
        else 
         throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(line).toString()); 
        out.writeShort(npcId); 
        out.writeShort(hitpoints); 
        out.writeShort(attackAnim); 
        out.writeShort(defenceAnim); 
        out.writeShort(deathAnim); 
        out.writeByte(attackDelay); 
        out.writeByte(deathDelay); 
        out.writeInt(respawnDelay); 
        out.writeShort(maxHit); 
        out.writeByte(attackStyle); 
        out.writeShort(attackGfx); 
        out.writeShort(attackProjectile); 
        out.writeByte(agressivenessType); 
        npcCombatDefinitions.put(Integer.valueOf(npcId), new NPCCombatDefinitions(hitpoints, attackAnim, defenceAnim, deathAnim, attackDelay, deathDelay, respawnDelay, maxHit, attackStyle, attackGfx, attackProjectile, agressivenessType)); 
       } 
      } while(true); 
      in.close(); 
      out.close(); 
     } 
     catch(FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    private static void loadPackedNPCCombatDefinitions() 
    { 
     try 
     { 
      RandomAccessFile in = new RandomAccessFile("data/npcs/packedCombatDefinitions.ncd", "r"); 
      FileChannel channel = in.getChannel(); 
      int npcId; 
      int hitpoints; 
      int attackAnim; 
      int defenceAnim; 
      int deathAnim; 
      int attackDelay; 
      int deathDelay; 
      int respawnDelay; 
      int maxHit; 
      int attackStyle; 
      int attackGfx; 
      int attackProjectile; 
      int agressivenessType; 
      for(ByteBuffer buffer = channel.map(java.nio.channels.FileChannel.MapMode.READ_ONLY, 0L, channel.size()); buffer.hasRemaining(); npcCombatDefinitions.put(Integer.valueOf(npcId), new NPCCombatDefinitions(hitpoints, attackAnim, defenceAnim, deathAnim, attackDelay, deathDelay, respawnDelay, maxHit, attackStyle, attackGfx, attackProjectile, agressivenessType))) 
      { 
       npcId = buffer.getShort() & 0xffff; 
       hitpoints = buffer.getShort() & 0xffff; 
       attackAnim = buffer.getShort() & 0xffff; 
       defenceAnim = buffer.getShort() & 0xffff; 
       deathAnim = buffer.getShort() & 0xffff; 
       attackDelay = buffer.get() & 0xff; 
       deathDelay = buffer.get() & 0xff; 
       respawnDelay = buffer.getInt(); 
       maxHit = buffer.getShort() & 0xffff; 
       attackStyle = buffer.get() & 0xff; 
       attackGfx = buffer.getShort() & 0xffff; 
       attackProjectile = buffer.getShort() & 0xffff; 
       agressivenessType = buffer.get() & 0xff; 
      } 

      channel.close(); 
      in.close(); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    private NPCCombatDefinitionsL() 
    { 
    } 

    private static final HashMap<Integer, NPCCombatDefinitions> npcCombatDefinitions = new HashMap<Integer, NPCCombatDefinitions>(); 
    private static final NPCCombatDefinitions DEFAULT_DEFINITION = new NPCCombatDefinitions(1, -1, -1, -1, 5, 1, 1, 0, 0, -1, -1, 0); 
    @SuppressWarnings("unused") 
    private static final String PACKED_PATH = "data/npcs/packedCombatDefinitions.ncd"; 
} 

テキストファイルでフォーマット: http://pastebin.com/ngrECkuD

+1

、アレックスは、しかし、その変数の正しい用語は次のようになり、ここであなたに行くを持っていない 'splitLine' 。動詞 "split"の過去時制は、 "私は昨日のログを分割する"のように、実際には "分割"されています。あなたは英語の単語ではない "分割"を使用しようとしているようで、いずれにせよ、英語の単語ではなく、基本単語を "分割"する "t"を見逃しています。とにかく、質問に全く関係ない、私はちょうど歩兵である:-) – paxdiablo

答えて

1

あなたが読んで文字列npcIdを解析しようとしているので、関連付けられた数はStringとして表されません。7344

おそらく意味:

int npcId = Integer.parseInt(splitedLine[1]); 

の代わり:あなたの質問を書くとき

int npcId = Integer.parseInt(splitedLine[0]); 

次回は、http://sscce.org/を参照してください。

+0

うん、今私はこれを得る –

+0

スレッド "main"でxception java.lang.RuntimeException:無効なNPC戦闘定義行:1、npcId - 7344 –

+0

@AlexDaSilva - 入力ファイルの#14行目に到達している可能性があります。これは、予想している2つではなく0つのフィールドがあります。あなたがさらに進んでいく前に、あなたのコードの各行が何をしているのかを理解する時間です。 – ziesemer

0

投稿した形式が正しくありません。ヒットポイントattackAnim defenceAnim deathAnim attackDelay deathDelay respawnDelay maxHit attackStyle attackGfx attackProjectile agressivenessType


//マン - - 2 < <この // npcId:

例:これは、パッカーが読むことができる形式ですここでnpcが何であるか教えてください。

1から70 422 425 836 5 1 60 10乱闘-1 -1 PASSIVE


関連する問題