string out of bounds error


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: string out of bounds error

  1. #1
    Join Date
    Apr 2006
    Posts
    20

    string out of bounds error

    Hi i am getting a very strange exception that i dont quit understand because when i debug the area in which the exception is thrown, no exception is thrown during debugging at the specific area.

    to make my problem simplier to understand, the exception points to this code below that gets some string value and splits into an array and than grab a part of the string value.

    Code:
    public String split(String original, String separator)
         {
             Vector nodes = new Vector();
             String tab = "";
    
            // Parse nodes into vector
            int index = original.indexOf(separator);
            while(index>=0) 
            {
                nodes.addElement( original.substring(0, index) );
                original = original.substring(index+separator.length());
                index = original.indexOf(separator);
            }
            // Get the last node
            nodes.addElement( original );
    
            // Create splitted string array
            String[] result = new String[ nodes.size() ];
            if( nodes.size()>0 ) 
            {
                for(int loop=0; loop<nodes.size(); loop++)
                result[loop] = (String)nodes.elementAt(loop);
            }
            ///////////////////////////////
            
            StringBuffer b = new StringBuffer();
            int length = 0;
            int j = 0;
            try
            {
                    int g = 0;
                    int q = 0;
                    
                        length = result[2].length();
                        
    
                        while(q < length)
                        {
                            char val = result[2].charAt(q);
                            if ((val == '"') && (g == 0) && (length != q))
                            {
    
                                //store digits
                                g = q;
                                char val1;
                                while((val1 = result[2].charAt(g+1)) != '"')  // exception error points here
                                {
                                    b.append(val1);
                                    g++;
                                }
    
                            }
                            q++;
                        }
                      g=0;
                      q=0;
                    
                    
                    tab = b.toString();
            
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
            
            return tab;
         }
    The following code is executed below in this method. look at the amount of times the method split() is executed. when debugging it, not one of them indicates an exception but has soon as i move to the next part of my code, the exception is thrown.


    Code:
    //get the values and prepare them
                 fname = "?sg_FirstName=" + split(data[8], " ") + "\n" + "\0";
                 lname = "&sg_LastName=" + split(data[9], " ") + "\n" + "\0";
                 address = "&sg_Address=" + split(data[10], " ") + "\n" + "\0";
                 city = "&sg_City=" + split(data[18], " ") + "\n" + "\0";
                 state1 = "&sg_State=" + "&" + "\n" + "\0";
                 ip = "sg_IPAddress=&" + "\n" + "\0";
                 zip = "sg_Zip=" + split(data[12], " ") + "\n" + "\0";
                 country = "&sg_Country=" + split(data[16], " ") + "\n" + "\0";
                 phone = "&sg_Phone=" + split(data[13], " ") + "\n" + "\0";
                 email = "&sg_Email=" + split(data[11], " ")+ "\n" + "\0";
                 cvv2 = "&sg_CVV2=" + card[0] + "\n" + "\0";
                 number = "&sg_CardNumber=" + card[1] + "\n" + "\0";
                 expM = "&sg_ExpMonth=" + card[2] + "\n" + "\0";
                 expY = "&sg_ExpYear=" + card[3] + "\n" + "\0";
                 amount = "&sg_Amount=" + card[4] + "\n" + "\0";
                 id = "&sg_ClientUniqueID=" + split(data[6], " ") + "\n" + "\0";
                 login = "&login=" + split(data[0], " ") + "\n" + "\0";
                 pass = "&pass=" + split(data[1], " ") + "\n" + "\0";
                 lang = "&lang=" + split(data[2], " ") + "\n" + "\0";
                 cur = "&cur=" + split(data[19], " ") + "\n" + "\0";
                 String sid = "id=1&" + "\n" + "\0";
                 String cart = "cartec=casino770" + "\n" + "\0";
                 
    //exception thrown at this point but why?
                 String url2 = "http://fcg.casino770.com/fcg-games/depot/mobileok.php3"
                         +fname+lname+address+city+state1+ip+zip+country+phone
                         +email+cvv2+number+expM+expY+amount+id+login+pass+lang
                         +cur+sid+cart;
     
                 
                 server2 = (HttpConnection) Connector.open(url2, Connector.READ_WRITE);
    Please note that the type of data thats contained in result[2] is "
    Code:
    value="glazlo">
    "

    Thanks for reading this and hope someone can help me thanks

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    what are the values of g, length, and q when the string out of bounds error occurs? What seems to me the most obvious point of going out of bounds is the "charAt(g+1)" which could actually attempt to read beyond the string since q < length could still have q at the last element of the string, then when you set g = q you're at the last element of the string then you try to read g+1 you are outside the bounds of the string.

    Change your test to q != length-1 and the error should be eradicated.
    Last edited by nspils; 05-03-2007 at 11:13 AM.

  3. #3
    Join Date
    Apr 2006
    Posts
    20
    like this?
    while(q != length-1)
    {

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I was thinking about this test (the if statement where you are getting your exception):

    if ((val == '"') && (g == 0) && (length != q))

    changing it to

    if ((val == '"') && (g == 0) && (q != length - 1))

    so that it is testing whether or not q is at the last element of the string
    Last edited by nspils; 05-03-2007 at 11:48 AM.

  5. #5
    Join Date
    Apr 2006
    Posts
    20
    nope its still the same im afraid. i still get that error

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    put some debugging statements into your code to output the values of q, g, result2 and the charAt(q) and charAt(g+1) to demonstrate to yourself the state of your program when the exception is generated

  7. #7
    Join Date
    Apr 2006
    Posts
    20
    the problem is when i debug the split method no exception is thrown. everything seems to work fine and has mentioned before. the exception is thrown just before the String url code

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    What I am suggesting is that you output to the console the current values of q, g, result, and all charAt() in this section. I understand that you are not seeing the exception when you run debug but you are seeing the exception during runtime so these are runtime checks to give you insight into the state of your program if and when an exception is encountered.

Similar Threads

  1. I can't link FreeImage to C#
    By mesh2005 in forum .NET
    Replies: 1
    Last Post: 03-06-2006, 09:16 AM
  2. Packed Data(Comp-3, etc)
    By Marcos in forum VB Classic
    Replies: 3
    Last Post: 01-25-2006, 12:18 PM
  3. App Object
    By Rob Teixeira in forum .NET
    Replies: 15
    Last Post: 05-31-2002, 04:30 PM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 10:16 PM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 04:21 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center