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

Finishing TreeStory tests don't seem to work properly?

Hello,

I'm having trouble I can't figure out in the Intermediate Java Track, Local Development Environments, Finishing TreeStory, Challenge page near the end, part of the course.

I have built and verified that my project works as requested in Intellij IDEA< but when I input my Main.java and Prompter.java contents, I first get this error when I try to submit:

Bummer: Whoops looks like you forgot to remove the TODO's after you completed them. This helps everyone know it is done

But I had removed all the TODOs. I tried removing the mention of TODOs in the comments section of the quiz and then get this error, which looks like it's not working for a console prompt when I run readLine.

java.lang.NullPointerException at com.teamtreehouse.Prompter.promptForStory(Prompter.java:65) at com.teamtreehouse.Main.main(Main.java:14) at JavaTester.run(JavaTester.java:77) at JavaTester.main(JavaTester.java:39)

Main.java

package com.teamtreehouse;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.io.Console;

public class Main {

    public static void main(String[] args) {
        Prompter prompter = new Prompter();
        String story = prompter.promptForStory();
        Template tmpl = new Template(story);
        prompter.run(tmpl);
    }
}

Prompter.java

package com.teamtreehouse;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

public class Prompter {
    private BufferedReader mReader;
    private Set<String> mCensoredWords;

    Console console = System.console();

    public Prompter() {
        mReader = new BufferedReader(new InputStreamReader(System.in));
        loadCensoredWords();
    }

    private void loadCensoredWords() {
        mCensoredWords = new HashSet<String>();
        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);
    }

    public void run(Template tmpl) {
        List<String> results = null;
        try {
            results = promptForWords(tmpl);
        } catch (IOException e) {
            System.out.println("There was a problem prompting for words");
            e.printStackTrace();
            System.exit(0);
        }
        String storyResults = tmpl.render(results);
        System.out.printf("Your TreeStory:%n%n%s%n%n", storyResults);
    }

    /**
     * Prompts user for each of the blanks
     *
     * @param tmpl The compiled template
     * @return
     * @throws IOException
     */
    public List<String> promptForWords(Template tmpl) throws IOException {
        List<String> words = new ArrayList<String>();
        for (String phrase : tmpl.getPlaceHolders()) {
            String word = promptForWord(phrase);
            words.add(word);
        }
        return words;
    }

    public String promptForStory() {
        String story = console.readLine(
                "Please enter a story line %n%n" +
                        "(Enter a sentence with placeholders, i.e.: Thanks __name__ for helping me out.  " +
                        "%n%n Placeholder Format: " +
                        "%n __name__ = a name" +
                        "%n __adjective__ = an adjective" +
                        "%n __noun__ = a noun" +
                        "%n and so on...  : ");
        return story;
    }

    /**
     * Prompts the user for the answer to the fill in the blank. Value is guaranteed
     * to be not in the censored words list.
     *
     * @param phrase The word that the user should be prompted. eg: adjective,
     *               proper noun, name
     * @return What the user responded
     */
    public String promptForWord(String phrase) {
        String mPhrase = phrase;
        boolean isInvalidWord = false;
        String word;
        do {
            word = console.readLine("Please enter a(n) %s:  ", mPhrase);
            for (String censoredWord : mCensoredWords) {
                isInvalidWord = word.equalsIgnoreCase(censoredWord);
                if (isInvalidWord) {
                    console.printf("That language is not allowed. Try again. \n\n");
                    break;
                }
            }
        } while (isInvalidWord);

        return word;
    }
}

I'm not sure what else to do to complete this course. Can someone help?

Thank you, -Brian

3 Answers

Steven Parker
Steven Parker
231,184 Points

Here's a couple of hints:

  • the code creates an "mReader" object but doesn't seem to use it anywhere (it should!)
  • the compiler messages from the "Preview" button can be helpful
Steven Parker
Steven Parker
231,184 Points

You're very close .. but there's a couple of places in the code where a new BufferedReader (named "br") is constructed by passing the current one (mReader) as an argument to the constructor. There are two issues with this:

  • the correct argument to construct a BufferedReader should be a Reader, not another BufferedReader
  • why construct a new one at all, when the class already has mReader ready?

Ok, I removed the mReader entry as I now use Console.readLine(). Is that different implementation what is causing the problem for me?

Should I be using the mReader = new BufferedReader(new InputStreamReader(System.in)); method for input for the tests to pass?

I think I just answered my own question. :) These test may not be able to access Console?

I'll try this fix.

Thank you, -Brian

Steven Parker
Steven Parker
231,184 Points

So do you have it working now?

Thanks for checking! I didn't get a lot of time to work on it, but I got it farther... Using the InputStreamReader seems to be the trick to this, but then I fail on the second input (the prompt for the word that fits into the template). Am I not using the InputStreamReader line correctly? Can't I reuse mReader like I am here?

And now the Preview output is:

Please enter a story line 

(Enter a sentence with placeholders, i.e.: Thanks __name__ for helping me out.:  
Please enter a(n) first prompt:  java.lang.NullPointerException
    at com.teamtreehouse.Prompter.promptForWord(Prompter.java:99)
    at com.teamtreehouse.Prompter.promptForWords(Prompter.java:58)
    at com.teamtreehouse.Prompter.run(Prompter.java:38)
    at com.teamtreehouse.Main.main(Main.java:15)
    at JavaTester.run(JavaTester.java:77)
    at JavaTester.main(JavaTester.java:39)

Main.java

package com.teamtreehouse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


public class Main {

    public static void main(String[] args) {
        Prompter prompter = new Prompter();
        String story = prompter.promptForStory();
        Template tmpl = new Template(story);
        prompter.run(tmpl);
    }
}

Prompter.java

package com.teamtreehouse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

public class Prompter {
    private BufferedReader mReader;
    private Set<String> mCensoredWords;

    // Console console = System.console();

    public Prompter() {
        mReader = new BufferedReader(new InputStreamReader(System.in));
        loadCensoredWords();
    }

    private void loadCensoredWords() {
        mCensoredWords = new HashSet<String>();
        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);
    }

    public void run(Template tmpl) {
        List<String> results = null;
        try {
            results = promptForWords(tmpl);
        } catch (IOException e) {
            System.out.println("There was a problem prompting for words");
            e.printStackTrace();
            System.exit(0);
        }
        String storyResults = tmpl.render(results);
        System.out.printf("Your TreeStory:%n%n%s%n%n", storyResults);
    }

    /**
     * Prompts user for each of the blanks
     *
     * @param tmpl The compiled template
     * @return
     * @throws IOException
     */
    public List<String> promptForWords(Template tmpl) throws IOException {
        List<String> words = new ArrayList<String>();
        for (String phrase : tmpl.getPlaceHolders()) {
            String word = promptForWord(phrase);
            words.add(word);
        }
        return words;
    }

    public String promptForStory() {
        BufferedReader br = new BufferedReader(mReader);
        String story;

        System.out.println("Please enter a story line \n\n" +
                        "(Enter a sentence with placeholders, i.e.: Thanks __name__ for helping me out.:  ");
        try {
            story = br.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return story;
    }

    /**
     * Prompts the user for the answer to the fill in the blank. Value is guaranteed
     * to be not in the censored words list.
     *
     * @param phrase The word that the user should be prompted. eg: adjective,
     *               proper noun, name
     * @return What the user responded
     */
    public String promptForWord(String phrase) {
        BufferedReader br = new BufferedReader(mReader);
        String mPhrase = phrase;
        boolean isInvalidWord = false;
        String word;
        do {
            System.out.printf("Please enter a(n) %s:  ", mPhrase);
            try {
                word = br.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            for (String censoredWord : mCensoredWords) {
                isInvalidWord = word.equalsIgnoreCase(censoredWord);
//                System.out.println("\n\n" + mPhrase + " " + isInvalidWord + " " + word);
                if (isInvalidWord) {
                    System.out.println("That language is not allowed. Try again. \n\n");
                    break;
                }
            }

        } while (isInvalidWord);

        return word;
    }
}