<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">// declaring arrays of objects
class Test {

    Test(){}
    void print(int i) {
	System.out.println("Test: "+i);

    } // method print
} // class Test

public class array_declare {
    public static void main(String args[]) {	

	// An array is an object with must
	//    be declared and instatiated.
	Test[] a = new Test[2];

	// If the array is an array of objects,
	//    each element must be instantiated
	//    before the element can be used:
	a[0] = new Test();
	a[1] = new Test();
	
	System.out.println("Testing correct method.");
	a[0].print(1);
	a[1].print(2);
	
	// What if you skip instantiating each
	//    element???
	Test[] b = new Test[2];

	System.out.println("Testing incorrect method.");
	b[0].print(1);
	b[1].print(2);

	// Do you believe me now?
	// EACH ELEMENT MUST BE INSTANTIATED.

    } // method main
} // class array_objects

/* Output:
Testing correct method.
Test: 1
Test: 2
Testing incorrect method.
java.lang.NullPointerException
        at array_declare.main(Compiled Code)
*/
</pre></body></html>