AP CSA Arrays Of Objects

Arrays of Objects in AP CSA

An array of objects stores references, not the objects themselves. Each element must be individually constructed with new, and calling methods on an uninitialized element causes a NullPointerException.

Declaring and Populating

// Creates an array of 3 Student references -- all null!
Student[] roster = new Student[3];

// Each element must be separately constructed
roster[0] = new Student("Ana", 11);
roster[1] = new Student("Ben", 10);
roster[2] = new Student("Cara", 12);

Calling Methods on Array Elements

for (int i = 0; i < roster.length; i++) {
    System.out.println(roster[i].getName());  // calls method on each Student
    System.out.println(roster[i].getGrade());
}
⚠ NullPointerException Risk: new Student[3] creates 3 null references, NOT 3 Student objects. Accessing roster[0].getName() before assigning roster[0] = new Student(...) throws a NullPointerException.

📝 Practice Question 1

After Car[] lot = new Car[5];, which of the following is true?

📝 Practice Question 2

What error occurs when the following code runs?

Book[] shelf = new Book[3];
System.out.println(shelf[0].getTitle());
✅ Exam Tip: The AP exam distinguishes between creating the array and creating the objects. Always check whether elements have been initialized before methods are called on them.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]