0

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
5
  • 5
    What is the expected form of fullName? When is it set? There WILL be an arrayOutOfBoundsException if there not at least 2 "@" symbols.. try/catch block or not. Commented Jan 11, 2012 at 3:28
  • I already tried putting 2 "@" symbols there and I still got an arrayOutOfBounds exception Commented Jan 11, 2012 at 3:38
  • Please post the code when you where putting them. Commented Jan 11, 2012 at 3:41
  • Added the stacktrace code and it says it is on the line of code: this.identity = subNames[1]; Commented Jan 11, 2012 at 3:59
  • What exactly is "fullName" like? Have you tried something like "a@b@c"? I think it would be helpful to have the complete stacktrace, which can be obtained by calling "Log.w(LOG_TAG, "Invalid profile", ex)" instead of "Log.w(... + ex)". Also, note that "".split("@").length -> 1, "@".split("@").length -> 0, and "@@".split("@") -> 0. Commented Jan 11, 2012 at 4:19

2 Answers 2

5

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.