2012-01-11 14 views
0

配列が範囲外になっています。 try/catchブロックで配列の値を設定しているので、なぜ配列が範囲外になっているのかわからない。配列インデックス外の例外 - Android

private static String profileName; 
private String identity = null; 
private String address = null; 
private boolean valid = false; 
private int profileId; 

public String fullName=""; 


private LinkedHashMap<String, String> nameValues = new 
LinkedHashMap<String, String>(0); 

public Profile(){ 
    try { 
     String[] subNames = fullName.split("@"); 
     this.profileName = subNames[0]; 
     this.identity = subNames[1]; 
     this.address = subNames[2]; 
     this.valid = true; 
    } catch (Exception ex) { 
     Log.w(LOG_TAG, "Invalid profile, " + ex); 
     this.valid = false; 
    } 
} 

置く値コード:ここで

public String getAttributeValue(String key){ 
    return nameValues.get(key); 
} 

public void addAttribute(String key, String value){ 
    nameValues.put(key, value); 
} 

は、私はこの問題から取得していますスタックトレースです:

01-11  01:37:20.721: I/ActivityThread(1759): Pub com.whooznear.android: com.whooznear.android.ProfileProvider 
01-11  01:37:21.142: D/dalvikvm(1759): GC_FOR_ALLOC freed 42K, 4% free 6337K/6595K, paused 139ms 
01-11  01:37:21.174: I/dalvikvm-heap(1759): Grow heap (frag case) to 6.805MB for 588816-byte allocation 
01-11  01:37:21.472: D/dalvikvm(1759): GC_CONCURRENT freed 8K, 4% free 6903K/7175K, paused 20ms+14ms 
01-11  01:37:24.974: D/dalvikvm(1759): GC_FOR_ALLOC freed 603K, 11% free 6666K/7431K, paused 81ms 
01-11  01:37:25.142: D/dalvikvm(1759): GC_CONCURRENT freed <1K, 4% free 7167K/7431K, paused 6ms+14ms 
01-11  01:37:25.642: W/Profile(1759): Invalid profile, java.lang.ArrayIndexOutOfBoundsException: index=1 length=1 
01-11  01:46:21.692: W/IInputConnectionWrapper(1759): showStatusIcon on inactive InputConnection 
01-11  01:46:25.836: D/dalvikvm(1759): GC_EXPLICIT freed 384K, 6% free 7110K/7559K, paused 9ms+5ms 
+5

fullName' 'の予想される形式は何ですか?それはいつ設定されますか?少なくとも2つの "@"記号がなければ、arrayOutOfBoundsExceptionが発生します。try/catchブロックかどうか。 – paislee

+0

すでに2つの "@"記号を入れてみましたが、まだarrayOutOfBounds例外があります – user268397

+0

_putting_それらの場所にコードを投稿してください。 – paislee

答えて

4

確かにProfileはコンストラクタであり、どのようにすればよくわかりませんfullName文字列が割り当てられていますか?しかし、使用する前に配列の境界/長さをチェックする必要があります。

てみてください、

String[] subNames = fullName.split("@"); 
if(subNames.length==3) { 
    this.profileName = subNames[0]; 
    this.identity = subNames[1]; 
    this.address = subNames[2]; 
    this.valid = true; 
} 
0

)(ex.printStackTraceをお試しください。配列外のエラーが発生している場所を特定することができます。