When splitting something, it's always recommended to check the result.
Before you do:
tokenValue = splitMetaDataValue[i].split(":");
Check the length of splitMetaDataValue
.
Question
getting java.lang.ArrayIndexOutOfBoundsException
this error please help.
Following is the code
public void extractData(String stringToExtract)
{
String metaDataValue = stringToExtract;
//System.out.println(metaDataValue);
String [] splitMetaDataValue = metaDataValue.split(",");
String [] tokenValue = null;
for (int i = 0; i<splitMetaDataValue.length ; i++)
{
tokenValue = splitMetaDataValue[i].split(":");
System.out.println(tokenValue[0]+"->"+tokenValue[1]);
}
}//extractData
Solution
When splitting something, it's always recommended to check the result.
Before you do:
tokenValue = splitMetaDataValue[i].split(":");
Check the length of splitMetaDataValue
.
OTHER TIPS
splitMetaDataValue[i].split(":").length
has to be 2 in order for the following to work:
System.out.println(tokenValue[0]+"->"+tokenValue[1]);
i.e.
tokenValue = splitMetaDataValue[i].split(":");
if (tokenValue.length > 1) {
System.out.println(tokenValue[0]+"->"+tokenValue[1]);
}
You need to check the size of String[] tokenValue. The lines
System.out.println(tokenValue[0]+"->"+tokenValue[1]);
must be causing ArrayIndexOutOfBounds exception because they are assumptions that splitMetaDataValue[i] will contain ":".
The expression splitMetaDataValue[i].split(":")
is not guaranteed to yield an array of 2 Strings
- check the array length before attempting to access its elements
if (tokenValue.length >= 2) {
System.out.println(tokenValue[0]+"->"+tokenValue[1]);
}