0
public float getAccountBalance()    {       //log.debug("in getAccountBalance");
    PostMethod method = new PostMethod(smsServiceUrl);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
    method.setParameter("Username", username);
    method.setParameter("PIN", PIN);
    method.setParameter("AvailableCredit", "");
    String result = new String();
    try {
        result = doHttpServiceRequest(method);
    //  log.debug("result is: " + result);
    } catch (Exception e) {
    //  log.warn(e.toString());
    }

    String[] retArray = result.split(" ");
    return Float.valueOf(retArray[1]);

}

Here i am getting ArrayIndexOutBoundException. can any one tell me how to rectify that exception?

Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
Deepu
  • 11
  • 6
  • Please format your code more readably when asking a question, and tell us the value of `result`. Presumably it doesn't contain a space. – Jon Skeet Nov 26 '13 at 08:58
  • Use return Float.valueOf(retArray[0]); – SweetWisher ツ Nov 26 '13 at 08:59
  • I would assume that `result` may not contain any spaces, meaning that split will create an array of size 1 (index 0). – Dragondraikk Nov 26 '13 at 08:59
  • 1
    Have you tried printing the content of the result variable ? – Adarsh Nov 26 '13 at 09:00
  • No. i just want to retrive the data present in the String array. & that will return that value to the calling env. – Deepu Nov 26 '13 at 09:12
  • possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Raedwald Mar 12 '15 at 14:02

4 Answers4

1

You're probably getting the exception here:

String[] retArray = result.split(" ");
return Float.valueOf(retArray[1]);

If you split according to " ", sometimes there might be no second element. You need to check that:

String[] retArray = result.split(" ");
if(retArray.length >= 2) {
   return Float.valueOf(retArray[1]);
}

Note that I write the condition only to demonstrate the issue. You might want to reconsider your logic. Also recall that arrays in Java are zero-based, when you return retArray[1], you're actually returning the second element in the array.

Maroun
  • 87,488
  • 26
  • 172
  • 226
0
String[] retArray = result.split(" ");
return Float.valueOf(retArray[1]);

will assume that there is at least two elements in the retArray - this may not be the case.
Test with retArray.length

Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
  • By doing this getting Exception like Exception in thread "main" java.lang.NumberFormatException: For input string: "-1&Report" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.valueOf(Unknown Source) at SMSServiceJava.CSoftHttpClientSMSService.getAccountMessageLimit(CSoftHttpClientSMSService.java:56) at SMSServiceJava.CSoftHttpClientSMSService.main(CSoftHttpClientSMSService.java:223) – Deepu Nov 26 '13 at 09:15
  • well read the error - is `1&Report` an Integer - NO ! Programming is not like mind reading. You have to be very precise is what you want your program to do. – Scary Wombat Nov 27 '13 at 00:05
0

I think you must check first, if array have any data or not

if(retArray.length>0)
{
  // do your thing
}
else
{
   // no element found
}
agpt
  • 4,887
  • 9
  • 45
  • 85
0

Check String result, either it will be empty or it will have data which doesn't have space delimiter also check the length of the retArray before access retArray[1] (by retArray[1] actually you are trying to access 2 element in the array.)

Prashant Thakkar
  • 1,193
  • 1
  • 11
  • 15