Originally posted by myself:
You are trying to iterate a List and telling it to find Person objects. But the List contains Book objects.
Yes, you are. You have put p1.getBooks() to the right of a : in a for-each loop, and Person aPerson to the left of the :. The getBooks() method returns a List<Book>, so you
are in fact trying to get Person objects out of a List of Books.
This is followed by a call to getBooks without the () and a null check.
Those first and second points both constitute compiler errors.
Also the null check is unnecessary; if you had encountered a null by now, you would have suffered a NullPointerException before that line.
When you are inside the for-each loop, you don't have an ArrayList object,
you should have Book objects. So your problem about getTitle() will be sorted out when you get the for-each loop right.
You need to find out how to write a for-each loop; you should find details in
the Java Tutorials. Remember if what is on the right of the : is supposed to contain books, whatever is on the left of the : will be Books. A for-each loop is also called an enhanced for loop.
There are also methods in the
List interface which allow you to add the entire contents of another List. You may find it easier to use them.