What Does "Array Dimension Missing" Mean in Java Programming?


When you begin Java development, there will inevitably be times when you encounter errors you do not immediately understand. The "Array Dimension Missing" error arises in cases where an array is not being instantiated correctly. The error is easy to fix, so it need not hold your development efforts back for long. Instantiation is the process of creating an object in Java. Understanding the cause of the dimension error is a key step in understanding how to use arrays and other objects in Java successfully.

Array Object

  • Arrays in Java are objects. This means that when your program creates an array, the Java Virtual Machine (JVM) allocates a section of memory for it. For this process to work, you need to specify information about the size of the array when your program creates it. Array dimensions determine their size. In a standard one-dimensional array, a single array dimension exists, representing the length -- which also corresponds to the number of element positions within the array.


  • Java programs can declare arrays without specifyng their dimensions. The following syntax example demonstrates declaring an array variable:

    int[] someNumbers;

    This code does not actually create the array; it simply declares the variable within the program. This allows code later in the flow of program execution to refer to the array, including the point at which the code creates and instantiates the array.


  • To instantiate an array object in Java, code uses the "new" keyword, which is the standard way to create object instances. The following sample code demonstrates an incorrect method of instantiating an array, which may cause the "Array Dimension Missing" error:

    int[] someNumbers = new int[];

    When you create a new array object, you must specify the length of the array, as in the following corrected syntax:

    int[] someNumbers = new int[20];

    The number included indicates the length of the array, providing Java with the information it needs to instantiate the array object.


  • Instantiating an array and doing the same for array elements are different activities. Once an array is instantiated, Java code can assign elements to specified positions within it. For example, the following code creates an array, then instantiates an element within it:

    Object[] someObjects = new Object[10];

    someObject[0] = new Object();

    It is possible to create and instantiate array elements in a single line of code as in the following example:

    String[] someWords = {"table", "chair", "shelf"};


  • Java can use arrays with more than one dimension. For example, the following code creates a two-dimensional array:

    int[][] lotsOfNumbers = new int[10][20];

    When Java programs create arrays with more than one dimension, they only initially need to specify the first dimension to avoid the "Array Dimension Missing" error:

    int[][] lotsOfNumbers = new int[10][];

    This works because multi-dimensional arrays are actually implemented as a series of separate arrays. If a programmer does not know what the length of the first array dimension is going to be when creating the array, she may do best to choose another collection class.

Related Searches


  • Photo Credit Photos.com/PhotoObjects.net/Getty Images
Promoted By Zergnet



Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!