Hello, I have tried making a merge sort method, but I am getting an Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 . How do I fix this error, I have tried but I can not find the error:

   public class MergeSort{

        public static int[] merge_sort(int[] b){
            if(b.length <= 1){
                return b;

            int midpoint = b.length/2;
            int[] left = new int[midpoint];
            int[] right;
            if(b.length %2 == 0){
                right = new int[midpoint];
                right = new int[midpoint + 1];

            int[] result = new int[b.length];
            for(int i = 0; i < midpoint; i++){
                left[i] = b[i];
            int x = 0;
            for(int j = midpoint; j < b.length; j++){
                if(x < right.length){
                    right[x] = b[j];
            left = merge_sort(left);
            right = merge_sort(right);
            result = merge(left, right);
            return result;

      public static int[] merge(int[] a, int[] b){
         int[] c = new int[a.length + b.length];
         int e = a[0];
         int g = b[0];
         int eI = 0;
         int gI = 0;
         for(int i = 0; i < c.length-2; i++){
            if(e <= g){
               c[i] = e;
               e = a[eI];
               c[i] = g;
               g = b[gI];

            if(a[eI] < b[gI]){
                c[c.length-2] = a[eI];
                c[c.length-1] = b[gI];
                c[c.length-2] = b[gI];
                c[c.length-1] = a[eI];
         return c;

      public static void main(String[] args){
         int[] a = {1,2,3};
            int[] b = {4,5,6,7,9};

        int[] c = merge_sort(a);


P.S: printArray() method has been cut out to reduce code.

4 Years
Discussion Span
Last Post by JamesCherrill

Lines 31 and 32 have arguments passed to the printArray method, but you haven't created a printArray method. Sorry, I just realized you left it out to reduce code. Maybe you should post it. Perhaps, your problem lies in it. I'm currently playing with your code. If I can figure it out. I'll let you know. :)

Edited by joshfizzle: Didn't realize he left code out on purpose


I recommend that you choose more meaningful variables names than a, b, c, e, g. It would make your merge method easier to read and would make finding the problem easier.

You haven't included the exact line number of your exception, but I notice that you do a[0] without checking that a contains any elements. The index of your exception is 1, not 0, but it's still odd that you don't check.

You also do eI++; e = a[eI]; without checking that eI + 1 < a.length. Surely that will lead to trouble.


How do I fix this error,

Wrong answer: Just thinking about it for more than about 30 seconds.
Wrong answer: Post it on DaniWeb without sharing the line number where the exception was thrown.

Right answer:
1. Get the line number where the exception is being thrown. See wha the array and index expression are
2. If there's more than 1 index expression on that line then print all the index expressions and corresponding array sizes to find out which it is.

Now you have some hard data you may be able to see the problem, or you could share that data here for more help.

Edited by JamesCherrill

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.