Trying to debug error arrayindexoutofbounds exception:4. (looked at other solutions no luck) : I'm trying to achieve this kind of output

**Enter size of list 1: 5**

**Enter items in list 1: 1 5 16 91 248**

**Enter size of list 2: 4**

**Enter items in list 2: 2 4 5 27**

**list1 is 1 5 16 91 248**

**list2 is 2 4 5 27**

**The merged list is 1 2 4 5 5 16 27 91 248**

What i'm getting is 

**Enter size of list 1: 5**

**Enter items in list 1: 1 5 16 91 248**

**Enter size of list 2: 4**

**Enter items in list 2: 2 4 5 27**

**list1 is 1 5 16 91 248**

**list2 is 2 4 5 27 Exception in thread"main" java.lang.ArrayIndexOutofBoundsException: 4 at MergeTest.main(Mergetest.java:41)**




I finished writing the code and now i'm lost because i'm getting an **Array indexoutOfBounds Exceptoin:4** and i already looked at other solutions people did and i'm still not getting it. Somebody told me it may be this part of the code but i'm still completely lost. 

    (a[firstIndex] <= b[secondIndex])
            {
                c[thirdIndex] = a[firstIndex];
                firstIndex++;
            }
            else
            {
                c[thirdIndex] = b[secondIndex];
                secondIndex++;
            }
            thirdIndex++;
            }"

 
Here is what i have so far.

    import java.util.Scanner;
    
    public class MergeTest{

    public static void main(String[] args){
        int firstLength, secondLength;
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the size of list 1: ");
        firstLength = keyboard.nextInt();
        int a[] = new int[firstLength];
        System.out.println("Enter the items of list 1: ");

        for (int i = 0; i < firstLength; i++)
        {
            a[i] = keyboard.nextInt();
        }

        System.out.print("Enter the size of list 2: ");
        secondLength = keyboard.nextInt();
        int b[] = new int[secondLength];
        System.out.println("Enter the items of list 2: ");

        for (int i = 0; i < secondLength; i++)
        {
            b[i] = keyboard.nextInt();
        }

        System.out.print("list1 is ");

        for (int i = 0; i < firstLength; i++) {
            System.out.print(a[i]);
            System.out.print(" ");
        }

        System.out.println();

        System.out.print("list2 is ");

        for (int i = 0; i < firstLength; i++) {
            System.out.print(b[i]);
            System.out.print(" ");
        }

        System.out.println();

        int thirdLength = firstLength + secondLength;
        int c[] = new int[thirdLength];
        int firstIndex = 0;
        int secondIndex = 0;
        int thirdIndex = 0;
        while (firstIndex < firstLength && secondIndex < secondLength) {

        if (a[firstIndex] <= b[secondIndex])
        {
            c[thirdIndex] = a[firstIndex];
            firstIndex++;
        }
        else
        {
            c[thirdIndex] = b[secondIndex];
            secondIndex++;
        }
        thirdIndex++;
        }
        System.out.print("Here is the output:");
        for (int z = 0; z < thirdLength; z++)
        {
            System.out.print(c[z]);
        }
    }
   }

Any feedback is appreciated.