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

Java

Java Data Structures Sets Challenge: java.lang.reflect.InvocationTargetException error

Here is my code for the challenge:

I am getting the error "java.lang.reflect.InvoationTargetException error".

Any thoughts?

package com.example;

import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class Blog {
  List<BlogPost> mPosts;

  public Blog(List<BlogPost> posts) {
    mPosts = posts;
  }

  public List<BlogPost> getPosts() {
    return mPosts;
  }

   public Set<String> getAllAuthors()
  {
    Set collect = new TreeSet<String>();
     for (String loop: getAllAuthors()){
      collect.add(loop);
     }

      return collect;
}

}

5 Answers

Hello,

I'm not sure immediately if this is causing the exception, but you don't include what your set is comprised of when you declare the set.

Set collect = new TreeSet<String>();

should be

Set<String> collect = new TreeSet<String>();
Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

I think you also want to loop through mPosts and use the getAuthor method on the BlogPost object.

Thanks both. I should have come back and replied sooner, my apologies. I restarted the challenge and passed. I was more interested in the type of error. I'd encountered it previously in another of the Efficiency challenges, and a brief google search seemed to me to suggest it might be caused by how I was naming my variables or similar.

Many thanks for both replies.

Thanks both. I should have come back and replied sooner, my apologies. I restarted the challenge and passed. I was more interested in the type of error. I'd encountered it previously in another of the Efficiency challenges, and a brief google search seemed to me to suggest it might be caused by how I was naming my variables or similar.

Many thanks for both replies.

Just to add. I finished the Data Structure Sets course last night. I've left feedback in the relevant places but wanted to say again what an awesome course it is. I found it very challenging, and that it really stretched me, but while being enjoyable and intriguing enough that I wanted to finish the course. Off to work through the Android courses now, but really looking forward to the next Java course! Keep up the excellent work!