/    Sign up×
Community /Pin to ProfileBookmark

Can anyone tell me what this error means and how I can fix it.

java.lang.StringIndexOutOfBoundsException: String index out of range: -190

Thanks for your help

to post a comment
Java

12 Comments(s)

Copy linkTweet thisAlerts:
@buntineSep 09.2004 — This exception subclasses IndexOutOfBoundsException. It is thrown by the String class when you try to reference an index (character position) that is less than zero, or in this case, greater than the length of the string.

You may want to show us the few lines of code around the line which threw the exception.

Regards,

Andrew Buntine.
Copy linkTweet thisAlerts:
@PtazauthorSep 09.2004 — ok..this is the script that is in the search page. Don't know if that is what you want. There is a java class that dose the sql function aswell as session.setAttribute("RecSource", rset);session.setAttribute("SearchFlag", "TRUE");session.setAttribute("Postal", fullPC.toString());.

<%

if (request.getSession().getAttribute("RecSource") != null) {

if (request.getSession().getAttribute("SearchFlag") != "FALSE" ) {

Vector rset = (Vector)request.getSession().getAttribute("RecSource");%>

<table width="98%" border="0" align="right" cellpadding="5" cellspacing="0">

<%if (!rset.isEmpty()){%>

<tr align="left" valign="top" bgcolor="#CCCC99">

<td class="mainbody"><STRONG>AGENT NAME</STRONG></td>

<td class="mainbody"><STRONG>ADDRESS</STRONG></td>

<td class="mainbody"><STRONG>POST<br />

CODE

</STRONG></td>

<td class="mainbody"><STRONG>TEL. NO.</STRONG></td>

</tr>

<%char Sep = '|';

for (int i=0; i < rset.size(); i++) {

String RowVal = rset.elementAt(i).toString();
int ColIndex1 = RowVal.indexOf(Sep);
String AgentName = RowVal.substring(0 , ColIndex1);
int ColIndex2 = RowVal.indexOf(Sep, ColIndex1 + 1);
String Address1 = RowVal.substring(ColIndex1 + 1, ColIndex2);
int ColIndex3 = RowVal.indexOf(Sep, ColIndex2 + 1);
String Address2 = RowVal.substring(ColIndex2 + 1, ColIndex3);
int ColIndex4 = RowVal.indexOf(Sep, ColIndex3 + 1);
String Postcode = RowVal.substring(ColIndex3 + 1, ColIndex4);
int ColIndex5 = RowVal.indexOf(Sep, ColIndex4 + 1);
String Phone = RowVal.substring(ColIndex4 + 1, ColIndex5);
if (i%2 != 0) {
%><TR><%
} else {
%><TR bgcolor="#FFFFCC"><%
}%>
<TD align="left" valign="top" class="mainbody"><%=AgentName.trim()%></TD>
<TD align="left" valign="top" class="mainbody"><%=Address1.trim() + " " + Address2.trim()%></TD>
<TD align="left" valign="top" class="mainbody"><%=Postcode.trim()%></TD>
<TD align="left" valign="top" class="mainbody"><%=Phone.trim()%></TD>
</TR>


Hope it helps.

Thanks for your help.

Ptaz
Copy linkTweet thisAlerts:
@buntineSep 09.2004 — The exception should tell you which line caused it. What does it say?

At a guess, I would say the exception is thrown on one of the [i]indexOf[/i] or [i]substring[/i] method calls.

Print the value of rset and comment out all of the String functions at th start of the For loop.

It should print out something like this: value|value|value|value|value

Ofcoarse, the word value will be replaced with an actual value in your program.

What does it print?

Regards.
Copy linkTweet thisAlerts:
@PtazauthorSep 09.2004 — sorry am a new to java, so where and how would i print this out? I didnt get any other error then the one I have listed. ?
Copy linkTweet thisAlerts:
@buntineSep 09.2004 — Try this.
<i>
</i>if (request.getSession().getAttribute("RecSource") != null)
{
if (request.getSession().getAttribute("SearchFlag") != "FALSE" )
{
Vector rset = (Vector)request.getSession().getAttribute("RecSource");
System.out.println("Vector: " + rset);
}
}

Regards.
Copy linkTweet thisAlerts:
@PtazauthorSep 09.2004 — tried that. It dosent work. i get jsp compile error.
Copy linkTweet thisAlerts:
@buntineSep 10.2004 — Weird. I think the problem may lie elsewhere... Unless JSP does not support System.out, which would be surprising.
<i>
</i>&lt;%
if (request.getSession().getAttribute("RecSource") != null)
{
if (request.getSession().getAttribute("SearchFlag") != "FALSE" )
{
Vector rset = (Vector)request.getSession().getAttribute("RecSource");
%&gt;
&lt;%= rset %&gt;
&lt;%
}
}
%&gt;

Does that error, too? What is the error?
Copy linkTweet thisAlerts:
@PtazauthorSep 13.2004 — no error. Just no records or lines being printed.

Would this to do with character string. where would one have defined this character strings?? e.g. in database filed mya have 30chars and then incereace to 50. Would you need to define this change in the java or something???
Copy linkTweet thisAlerts:
@Khalid_AliSep 13.2004 — the first place to look for an error is the lines below

[b]

int ColIndex1 = RowVal.indexOf(Sep);

String AgentName = RowVal.substring(0,ColIndex1);

[/b]


my first guess will be that ColIndex1 is not returning something that it should hence the RowVal.substring is throwing the exception....make sure that ColIndex1 as a value that can be used with substring
Copy linkTweet thisAlerts:
@PtazauthorSep 13.2004 — Thanks, that helps to see where I may be going wrong and now can say that it could be one of 2 things.

Would a NULL field in a record give this error?

The colIndex1 has company name, but on a previous database this field has 30 characters max but on the new one there are 50. same for the address field they used to be 30 on the previous database field but on the new its 60. Would this difference in the character field throw out the error. And how can I fix it?
Copy linkTweet thisAlerts:
@Khalid_AliSep 13.2004 — [i]Originally posted by Ptaz [/i]

[B]

Would a NULL field in a record give this error?

[/B]
[/QUOTE]


Most certainly.

[i]...the character field throw out the error. And how can I fix it? [/B][/QUOTE]



Seriously doubt that.

the only reason you will get problems are if

int ColIndex1 = RowVal.indexOf(Sep);

does not return a valid value for substring function to process and return what you are asking for.
Copy linkTweet thisAlerts:
@PtazauthorSep 15.2004 — ok..I got the <%=rset%> but It still not returning any data. now if I use the same code with another table(same data, but different character strings), it displays the recods.

What am I doing wrong?? How can I check and fix the Indexof? or increase the character strings so that it displays properly.
×

Success!

Help @Ptaz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...