2 Replies Latest reply: Jul 10, 2013 8:01 AM by 939520 RSS

    ArrayIndexOutOfBounds Exception - Not sure why?

    jtyreman

      Hi all getting the following error message:

       

      [B@2ca6d51eException in thread "main" java.lang.ArrayIndexOutOfBoundsException

        at java.lang.System.arraycopy(Native Method)

        at JDBCTest.main(JDBCTest.java:76)

       

      the lines it are reffering to are as follows:

       

        try {
      
      
                     stmt = con.createStatement();
      
      
                     rs = stmt.executeQuery("SELECT * FROM GD_BRANCH");
                 }
                 catch (SQLException sqle) {
      
      
                     System.out.println("ERROR: " + sqle.getMessage());
                 }
               ////// FETCH AND DISPLAY THE RESULTSET //////
      
      
               try {
      
      
                 // Print column labels, padded to correct display size.
                 for (int c=1; c<=rs.getMetaData().getColumnCount(); ++c)
                 {
                    byte[] buf = new byte[rs.getMetaData().getColumnDisplaySize(c) + 1];
      
      
                    System.arraycopy(rs.getMetaData().getColumnLabel(c).getBytes(), 0, buf, 0, rs.getMetaData().getColumnLabel(c).length());
      
      
                    if (rs.getMetaData().getColumnLabel(c).length() < buf.length)
                    {
                        int i;
      
      
                        for (i=rs.getMetaData().getColumnLabel(c).length(); i<buf.length; i++)
                        {
                            buf[i] = (byte)0x2E; // pad with "."s
                        }
      
      
                        buf[i-1] = (byte)0x20; // overwrite last byte with space " "
                    }
      
      
                    System.out.write(buf,0,buf.length);
                  }
      
      
                  System.out.println();
      
      
                  int numRows =0;
      

       

      Can anyone tell me why this error would occur?

       

      Thanks

        • 1. Re: ArrayIndexOutOfBounds Exception - Not sure why?
          rp0428

          >

          Can anyone tell me why this error would occur?

          >

          Only the obvious reason given by the exception

          >

          B@2ca6d51eException in thread "main" java.lang.ArrayIndexOutOfBoundsException

          at java.lang.System.arraycopy(Native Method) 

            at JDBCTest.main(JDBCTest.java:76)

          >

          We have no idea which is 'line 76' or what array index values you are using.

           

          You need to conduct your own basic troubleshooting. Review the array index values being used by either using a GUI debugger or by printing out the values.

          >

          System.arraycopy(rs.getMetaData().getColumnLabel(c).getBytes(), 0, buf, 0, rs.getMetaData().getColumnLabel(c).length());  

          >

          We have no idea what the size of 'buf' is or what 'length' value is being used. Look at the values being used and the problem should be obvious.

          • 2. Re: ArrayIndexOutOfBounds Exception - Not sure why?
            939520

            On an unrelated observation:

            Your code may work fine if the column name is less than the max char size and column type is varchar.

            What if the column name is equal to max char size? Can it handle it?

            What if the column name is greater than max char size? Can it handle it?

            What if the column type is something other than varchar such as char, int, date? Can it handle it?