www.webdeveloper.com
Results 1 to 13 of 13

Thread: Java

  1. #1
    Join Date
    Apr 2004
    Posts
    30

    Java

    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

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    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.

  3. #3
    Join Date
    Apr 2004
    Posts
    30
    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

  4. #4
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    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.

  5. #5
    Join Date
    Apr 2004
    Posts
    30
    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.
    Last edited by Ptaz; 09-09-2004 at 09:25 AM.

  6. #6
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Try this.
    Code:
    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.

  7. #7
    Join Date
    Apr 2004
    Posts
    30
    tried that. It dosent work. i get jsp compile error.

  8. #8
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Weird. I think the problem may lie elsewhere... Unless JSP does not support System.out, which would be surprising.
    Code:
    <%
    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?

  9. #9
    Join Date
    Apr 2004
    Posts
    30
    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???

  10. #10
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    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
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  11. #11
    Join Date
    Apr 2004
    Posts
    30
    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?
    Last edited by Ptaz; 09-13-2004 at 10:55 AM.

  12. #12
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    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.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  13. #13
    Join Date
    Apr 2004
    Posts
    30
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles