Results 1 to 6 of 6
  1. #1
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Question Suprising ArrayIndexOutOfBoundsException!!!!!!

    I am having a method here that throws an ArrayIndexOutOfBoundsException , here is the code:
    Java Code:
    /**
         * Gets the students ID 
         * @param regNumber the registration number of the student
         * @return the students ID, something like "7273/2008" out of E35/7273/2009
         */
        public String getStudentID(String reg_number){  
            this.regNumber = this.getValidRegNumber(reg_number);//   << E35/7273/2008
          /*   if a student enters e35/7273/2008 as the registration number
               getValidRegNumber( ) method formats it with appropriate formats,
               i.e the first letter must be in uppercaseE35/7237/2008.
               In some cases, the registration regNumber includes an 's' to denote a self   
               sponsered student,this s is always a lowercase character so an entry   
              e35s/6363/2009 would be return as E35s/6363/2009.
           */
            String split [] = regNumber.split("/");
            this.course = split[0];  //not important to this method, but just so you get the picture, 
                                      //this(E35) is a course code in our university
            studentID = split[1];
            yearOfEntry = split[2];
          return studentID.concat("/").concat( yearOfEntry);
        }
    Now the above method is called by a method registerToEstudent( ) located in the same class, to get the studentID and pass it to the database as part of the student details.I have even created a test class shown below and it works fine but no matter what i do, the above throws an ArrayIndexOutOfBound Exception.

    Code of the test class

    Java Code:
    package estudent;
    /**
     *
     * @author Moin' Victor <yourEmail_Is@myDomain.Hacked>
     */
    public class TestSplit {
      public static void main(String args[]){
             String regNumber = "E35/7732/2008";
             String split []= regNumber.split("/");
             String studentId = split[1];
             System.out.println(studentId.concat("/").concat(split[2]));
    
           
        }
    
    }
    And here is the exception for the first code
    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
            at estudent.KenyattaUniversityStudent.registerToEstudent(KenyattaUniversityStudent.java:336)
            at estudent.TestConfirmRegistration.main(TestConfirmRegistration.java:35)
    Java Result: 1

    Am I supposed to condemn this method as one that throws an exception like the case in InputStreamReader(System.in) wich throws IOException?And if that's the case, what happens to the method that depends on it, i.e how do i handle the exception so that registerToEstudent( ) still proceeds with what will be returned by getStudentID( )?Better stii, if their is a better alternative to do this that outwits split( ), just let me know, I'll be more that glad to delve into it.Any ideas????
    Last edited by nmvictor; 11-18-2009 at 11:26 AM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    The error suggests that the exception is thrown in registerToEstudent(). Where is that method?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Default

    The error suggests that the exception is thrown in registerToEstudent(). Where is that method?
    @PhHein
    I indicated in the explanation that the method registerToEstudent( ) is in the same class which is KenyattaUniversityStudent ,sorry if you missed that, just scrutinize the exception code above.

  4. #4
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Default

    The error suggests that the exception is thrown in registerToEstudent(). Where is that method?
    @PhHein
    I indicated in the explanation that the method registerToEstudent( ) is in the same class which is KenyattaUniversityStudent ,sorry if you missed that, just scrutinize the exception code above.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What you are missing is that the registerToEstudent method is the one that should be discussed in order for the exception to be stopped.

  6. #6
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    just go to line 35 of that class and look at it add line numbers to your viewer
    and then go to line 336 of your other class are these methods or objects talking to each other?


    throwing exceptions only helps if you catch them ....use a try catch on the line 336 method and use some system out additions to see which array is going haywire use a system.out on each iteration of the array for loop


    the array out of bounds error is thrown because there is a logical problem or mathematical problem with your code, it is trying to look for an item in the array and the array doesn't have that many items......it is probably a problem with 1's and 0's or whether it shouold be < or <= in one of the for loops
    Last edited by aaroncarpet; 11-18-2009 at 10:32 PM.

Similar Threads

  1. ArrayIndexOutOfBoundsException
    By flaskvacuum in forum New To Java
    Replies: 6
    Last Post: 07-14-2009, 05:36 PM
  2. [SOLVED] ArrayIndexOutOfBoundsException
    By thelinuxguy in forum Advanced Java
    Replies: 1
    Last Post: 05-07-2009, 10:34 PM
  3. java.lang.ArrayIndexOutOfBoundsException
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 07:29 AM
  4. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 05:16 PM
  5. ArrayIndexOutOfBoundsException
    By daredavil82 in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 09:29 PM

Posting Permissions

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