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
Edith England
4,270 PointsJava 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
James Simshaw
28,738 PointsHello,
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
Treehouse TeacherI think you also want to loop through mPosts and use the getAuthor method on the BlogPost object.
Edith England
4,270 PointsThanks 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.
Edith England
4,270 PointsThanks 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.
Edith England
4,270 PointsJust 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!