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 Local Development Environments Advanced Tooling Finishing TreeStory

Complete TreeStory resources/censored_words.txt not found. java.nio.file.NoSuchFileException

I am trying to get the downloaded version of TreeStory to work. I started working through the TODOs and added one line for Prompter in Main.java.

public static void main(String[] args) {
// write your code here
    // TODO:csd - Instantiate a new Prompter object and prompt for the story template
    Prompter prompter = new Prompter(); //Added line of code
    String story = "Thanks __name__ for helping me out.  You are really a __adjective__ __noun__ and I owe you a __noun__.";
    Template tmpl = new Template(story);
    // TODO:csd - Use the prompter object to have it do the prompting, censoring and outputting.  Call Prompter.run
    List<String> fakeResults = Arrays.asList("friend", "talented", "java programmer", "high five");
    // TODO:csd - This should really happen in the Prompter.run method, let's get these implementation details out of the main method
    String results = tmpl.render(fakeResults);
    System.out.printf("Your TreeStory:%n%n%s", results);
}

I tried to compile it having added the Prompter prompter = new Prompter(); line of code and I get an error:

Couldn't load censored words java.nio.file.NoSuchFileException: \resources\censored_words.txt at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230) at java.nio.file.Files.newByteChannel(Files.java:361) at java.nio.file.Files.newByteChannel(Files.java:407) at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384) at java.nio.file.Files.newInputStream(Files.java:152) at java.nio.file.Files.newBufferedReader(Files.java:2784) at java.nio.file.Files.readAllLines(Files.java:3202) at java.nio.file.Files.readAllLines(Files.java:3242) at com.teamtreehouse.Prompter.loadCensoredWords(Prompter.java:28) at com.teamtreehouse.Prompter.<init>(Prompter.java:19) at com.teamtreehouse.Main.main(Main.java:12) Exception in thread "main" java.lang.NullPointerException at java.util.AbstractCollection.addAll(AbstractCollection.java:343) at com.teamtreehouse.Prompter.loadCensoredWords(Prompter.java:33) at com.teamtreehouse.Prompter.<init>(Prompter.java:19) at com.teamtreehouse.Main.main(Main.java:12)

Process finished with exit code 1

It seems like just the call to load in the words is not working out of the box. Has anyone else had this issue or any ideas of what I can try? The censored_words.txt file is in the resources folder. Thank you!

1 Answer

Mihai Craciun
Mihai Craciun
13,520 Points
Couldn't load censored words java.nio.file.NoSuchFileException: \resources\censored_words.txt

Because your file does not exist it could not be loaded. Try create the file first and then rerun the code Or just comment these lines until your code is finished, add manually some bad words in the mCensoredWords List and then when you submit your finished code undo the process (uncomment the lines below and delete the rows you added for bad words)

Here is the code you could comment:

Path file = Paths.get("resources", "censored_words.txt");
        List<String> words = null;
        try {
            words = Files.readAllLines(file);
        } catch (IOException e) {
            System.out.println("Couldn't load censored words");
            e.printStackTrace();
        }
        mCensoredWords.addAll(words);

I tried removing and readding the file but that did not work. I was able to manually add a few censored words to the hash set and comment out the loading code and that worked for me to be able to run it locally.