Results 1 to 13 of 13
- 06-02-2011, 11:54 AM #1
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
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,
- 06-02-2011, 12:17 PM #2
Have a look : StringIndexOutOfBoundsException
Show the code if possible.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-02-2011, 01:25 PM #3
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
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();
}
- 06-02-2011, 02:07 PM #4
Did you look at line 94 to see why the arg to substring() is -1?StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1945)
at MessageSender.processMessage(MessageSender.java:94 )
- 06-03-2011, 06:52 AM #5
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
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
- 06-03-2011, 07:15 AM #6
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.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-03-2011, 07:44 AM #7
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
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
- 06-03-2011, 07:52 AM #8
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?
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-03-2011, 07:54 AM #9
- 06-03-2011, 09:44 AM #10
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
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-03-2011, 10:10 AM #11
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Thanks Goldest for all your help.
I am doing it.I will update.
- 09-28-2011, 07:25 PM #12
Member
- Join Date
- Sep 2011
- Posts
- 1
- Rep Power
- 0
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...........
- 09-28-2011, 07:32 PM #13
Similar Threads
-
Array index out of range: 1 HELP!
By TheBreadCat in forum New To JavaReplies: 2Last Post: 02-17-2011, 08:50 PM -
java.sql.SQLException: Column Index out of range, 3 > 1.
By chathura992 in forum JDBCReplies: 2Last Post: 01-07-2011, 11:50 AM -
String index out of range: -1 Please Help
By davetheant in forum New To JavaReplies: 22Last Post: 09-11-2010, 12:32 AM -
Error: svn: mergeinfo for 'oldpath/someclass.java' maps to an empty revision range
By maskellp in forum IntelliJ IDEAReplies: 0Last Post: 07-12-2010, 11:57 AM -
find index of string in another string
By Sdannenberg3 in forum New To JavaReplies: 4Last Post: 03-04-2010, 10:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks