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! While you're at it, check out some resources Treehouse students have shared here.

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


Kaho Kang
Kaho Kang
12,383 Points

Why this "authoredTreets" could be null, if we use the "get()" in Map ?

Map<String, List<Treet>> treetsByAuthor = new HashMap<String, List<Treet>>();

for (Treet treet : treets) {
  List<Treet> authoredTreets = treetsByAuthor.get(treet.getAuthor());
  if (authoredTreets == null) {
    authoredTreets = new ArrayList<Treet>();
    treetsByAuthor.put(treet.getAuthor(), authoredTreets);

System.out.printf("Treets by author:  %s %n", treetsByAuthor);
System.out.printf("Treets by Kaho:  %s %n", treetsByAuthor.get("kaho")); 

I thought we already created all the Treet objects, so if we use get(treet.getAuthor()), Wouldn't it just get all the authorNames that we added to the objects? So why there are possibility the authoredTreets is null? And even it is null and we create a new array list, then add it to the TreetsByAuthor, the array list is still empty isn't it? why use add(treet)? if is null, then what are we actually adding? I feel confused about this part....