"Laravel 4 Basics" was retired on January 24, 2020.

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 - Retired Exploring the Java Collection Framework Sets

Jean Malan
Jean Malan
10,781 Points

Sets Objective - Error: java.lang.reflect.InvocationTargetException

Hey everyone!

Not sure where I have gone wrong and I really don't get the error message that it's giving me - Error: java.lang.reflect.InvocationTargetException

Thanks in advance :)

com/example/BlogPost.java
package com.example;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

public class BlogPost implements Comparable<BlogPost>, Serializable {
  private String mAuthor;
  private String mTitle;
  private String mBody;
  private String mCategory;
  private Date mCreationDate;

  public BlogPost(String author, String title, String body, String category, Date creationDate) {
    mAuthor = author;
    mTitle = title;
    mBody = body;
    mCategory = category;
    mCreationDate = creationDate;
  }

  public int compareTo(BlogPost other) {
    if (equals(other)) {
      return 0;
    }
    return mCreationDate.compareTo(other.mCreationDate);
  }

  public String[] getWords() {
    return mBody.split("\\s+");
  }

  public List<String> getExternalLinks() {
    List<String> links = new ArrayList<String>();
    for (String word : getWords()) {
      if (word.startsWith("http")) {
        links.add(word);
      }
    }
    return links;
  }

  public String getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  public String getBody() {
    return mBody;
  }

  public String getCategory() {
    return mCategory;
  }

  public Date getCreationDate() {
    return mCreationDate;
  }
}
com/example/Blog.java
package com.example;

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Date;
import java.util.Set;
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<String> sortedAuthors = new TreeSet<String>();
    for (BlogPost blogPost : mPosts) {
      sortedAuthors.add(blogPost.getAuthor());
       System.out.printf("Authors: %S %S", sortedAuthors);
    }

    return null;

}

}

2 Answers

Richard Lambert
PLUS
Richard Lambert
Courses Plus Student 7,180 Points

Hello mate,

The utterly mind-boggling world of Java reflection is taught on the Java Turbo Wizard course. I'm going to hazard a guess and say that this particular exception is being thrown as a consequence of the way the underlying Treehouse test engine processes code entered when completing a task. Don't worry about the exception being thrown; instead, focus on these two issues (noted in code):

  1. Consider the format specifiers declared within the format string, and the type and number of objects being passed into them. For the task set, is this statement necessary?
  2. getAllAuthors() has correctly been declared as returning an object of type Set<String>. Is this reflected within the body of the method?
public Set<String> getAllAuthors() {
    Set<String> sortedAuthors = new TreeSet<String>();
    for (BlogPost blogPost : mPosts) {
        sortedAuthors.add(blogPost.getAuthor());
        System.out.printf("Authors: %S %S", sortedAuthors); // (1)
    }
    return null; // (2)
}

Hope this helps

Jean Malan
Jean Malan
10,781 Points

Thanks man! That was, although initially confusing to understand, pretty spot on!

Jean Malan
Jean Malan
10,781 Points

Not too sure what that Java Turbo Wizard link was though? haha

Fahad Mutair
Fahad Mutair
10,359 Points

hi Jean Malan , you got Invocation target exception from this printf , so better to remove it.

Blog.java
System.out.printf("Authors: %S %S", sortedAuthors);

also don't forget to return your set for that method

Blog.java
return null;
Fahad Mutair
Fahad Mutair
10,359 Points

Hello, i used 3 backticks then the name of that file followed by file type 3''Blog.java Then type the line you want