Click to See Complete Forum and Search --> : Java


Ptaz
09-09-2004, 04:36 AM
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

buntine
09-09-2004, 07:27 AM
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.

Ptaz
09-09-2004, 08:01 AM
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

buntine
09-09-2004, 10:11 AM
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 indexOf or substring 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.

Ptaz
09-09-2004, 10:19 AM
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. :(

buntine
09-09-2004, 11:53 AM
Try this.

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.

Ptaz
09-09-2004, 12:32 PM
tried that. It dosent work. i get jsp compile error.

buntine
09-09-2004, 09:50 PM
Weird. I think the problem may lie elsewhere... Unless JSP does not support System.out, which would be surprising.

<%
if (request.getSession().getAttribute("RecSource") != null)
{
if (request.getSession().getAttribute("SearchFlag") != "FALSE" )
{
Vector rset = (Vector)request.getSession().getAttribute("RecSource");
%>
<%= rset %>
<%
}
}
%>

Does that error, too? What is the error?

Ptaz
09-13-2004, 08:05 AM
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???

Khalid Ali
09-13-2004, 11:18 AM
the first place to look for an error is the lines below


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


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

Ptaz
09-13-2004, 11:50 AM
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?

Khalid Ali
09-13-2004, 01:00 PM
Originally posted by Ptaz

Would a NULL field in a record give this error?


Most certainly.

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

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.

Ptaz
09-15-2004, 12:33 PM
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.