user268397
user268397

Reputation: 1927

Array Index Out of Bounds Exception - Android

I'm getting an array out of bounds error. I'm setting the array values in the try/catch block so I'm not really sure why I'm getting an array out of bounds error.

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;
    }
}

Putting Value Code:

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

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

Here is the stack trace I'm getting from this issue:

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

Upvotes: 0

Views: 6236

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94645

I/s sure Profile is constructor and not sure how and when fullName string is assigned? However you must have to check bounds/length of an array before you use it.

Try,

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

Upvotes: 5

jency
jency

Reputation: 377

Try ex.printStackTrace(); and you can identify where you are getting an array out of bounds error.

Upvotes: 0

Related Questions