Results 1 to 13 of 13
  1. #1
    ashishb4 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default String index out of range: -1 Error.

    While getting a response from approver authority , sometimes I am getting following error:
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1945)
    at MessageSender.processMessage(MessageSender.java:94 )
    at MultiServerThread.run(MultiServerThread.java:59)
    Please let me know the handling and reason.

    Thanks,

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Have a look : StringIndexOutOfBoundsException

    Show the code if possible.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    ashishb4 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Hi,

    Thanks for the response.
    Inside the try block sendMessageOverHTTP function gets called.sendMessageOverHTTP function does handshake,then request and the gets the response,but I feel some error is returned from card side and then catch prints this:
    Handshake to Card : [2011-05-22 13:32:58.169]
    Request to Card : [2011-05-22 13:32:58.204]
    Response from Card : [2011-05-22 13:33:29.391]
    java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1945)
    at MessageSender.processMessage(MessageSender.java:94 )
    at MultiServerThread.run(MultiServerThread.java:59)

    Following is the code:


    try {
    -----------
    -----------
    /***********************Sending message to HTTP Server*************************/
    msgResponse = sendMessageOverHTTP(DataBean.getUrl(), signedMessage);
    response = msgResponse;
    System.out.println("Response from SERVER is: "+ response);
    } catch (Exception ex) {
    ex.printStackTrace();
    response = "Java Exception:"+ex.toString();
    }
    return response;
    }
    /************************************************** *********************/


    private String sendMessageOverHTTP(String urlstr, String msg) throws Exception
    {
    URL url = new URL(urlstr);
    String protocol = url.getProtocol();
    HTTPRequest req = new HTTPRequest(urlstr);
    req.setContent(msg.getBytes());
    req.setContentLength(msg.length());
    HTTPTransaction trans = null;
    System.out.println("Handshake to Card : [" + DataBean.getCurrentTimeStamp() + "]");
    if (protocol.equals("http"))
    trans = new HTTPTransaction();
    else
    trans = new HTTPTransaction(DataBean.getKeyStoreFile(), DataBean.getKeyStorePassPhrase().toCharArray(),
    DataBean.getPassPhrase().toCharArray(), DataBean.getKeyStoreFile(),
    DataBean.getKeyStorePassPhrase().toCharArray(), new HTTPRevision("1.1"));
    System.out.println("Request to Card : [" + DataBean.getCurrentTimeStamp() + "]");
    HTTPResponse res = trans.post(req, true);
    System.out.println("Response from Card : [" + DataBean.getCurrentTimeStamp() + "]");
    return res.toString();
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1945)
    at MessageSender.processMessage(MessageSender.java:94 )
    Did you look at line 94 to see why the arg to substring() is -1?

  5. #5
    ashishb4 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks Norm,

    The problematic line is:

    logResponse = logResponse.substring(j,logResponse.length());

    I think in substring while I am checking length,it is returning -1,

    Could you please tell me what does it mean and how to handle it and throw proper exception.

    Thanks,
    Ashish

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Check how you String logResponse is getting operated on. Print the string value before making the substring.

    And what is the "j" by the way. Print that value as well. Who knows, even that "j" can also be -1.

    Check it out and let us know.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    ashishb4 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Please have a look to the following code,it shows what is "J",

    /***********************Sending message to HTTP Server*************************/
    msgResponse = sendMessageOverHTTP(DataBean.getUrl(), signedMessage); //Send Message to HTTP Server
    /***********************Sending message to HTTP Server*************************/

    /***********************Removing AVV Number From the Response During Log*************************/
    logResponse = msgResponse;

    int j = logResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //find seperator
    logResponse = logResponse.substring(j,logResponse.length());
    msgResponse = logResponse;

    I can not reproduce the issue , as I need to deploy it, and until I handle it gracefully I can not deploy it.
    Could you please let me know the handling,then I will deploy with the handling and all required print statements.


    Thanks,
    Ashish

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    This is what the API documentation says about indexOf :

    if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.

    Any clue from where the -1 is coming and why?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by ashishb4 View Post
    Please have a look to the following code,it shows what is "J",
    Why should we? You have been given advice as to what is the possible cause of the exception. It is your code and therefore the onus is upon you to debug it. Why should we do it for you?

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Ashish,

    We have literally reached to the root cause of your problem. Now it's entirely up to you to crosscheck and resolve it. I wish you would do it without any issues from here onwards.

    And why to wait for someone else to solve your problem, when you know where the problem is. Go for it

    Goldes
    t
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  11. #11
    ashishb4 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks Goldest for all your help.
    I am doing it.I will update.

  12. #12
    shanto_0321 is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default Re: String index out of range: -1 Error.

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(String.java:687)
    at Ha.Main.main(Main.java:21)
    why this error have been occured?please help me...........

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: String index out of range: -1 Error.

    .StringIndexOutOfBoundsException: String index out of range: 1
    why this error have been occured?
    The String has less than 2 characters and the index value of charAt is 1. The first character would be at 0.

Similar Threads

  1. Array index out of range: 1 HELP!
    By TheBreadCat in forum New To Java
    Replies: 2
    Last Post: 02-17-2011, 09:50 PM
  2. Replies: 2
    Last Post: 01-07-2011, 12:50 PM
  3. String index out of range: -1 Please Help
    By davetheant in forum New To Java
    Replies: 22
    Last Post: 09-11-2010, 01:32 AM
  4. Replies: 0
    Last Post: 07-12-2010, 12:57 PM
  5. find index of string in another string
    By Sdannenberg3 in forum New To Java
    Replies: 4
    Last Post: 03-04-2010, 11:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •