Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Exploring the Java Collection Framework Maps

what's the purpose of authoredTreets = new ArrayList<Treet>();

I don't quite understand this certain line of code "authoredTreets = new ArrayList<Treet>();" Craig uses it for an if statement in case if the authoredTreets returns null. What does it do in this specific situation and what it tries to accomplish?

2 Answers

You want to add a treet to a list, if the list already exists it's not a problem to add to it. But if it the list doesn't yet exist, say it's the first treet from an author, then you need to create it prior to using the command add. If you don't create and you call add on nothing (null) them you get an exception.

In other words:

  1. Does a list not exist? Create one.
  2. Add to it. (Not a problem, the list will definitely exist due to the null check)

Thank you very much, Pedro. Now I finally understood it!