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

Doli Harahap
Doli Harahap
6,246 Points

Keep getting OOM or NullPointer. Which just doesn't make sense.

Hi All,

I keep getting OOM with this code.

My 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) {
    // write your code here
    Prompter prompter = new Prompter();
    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);

    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.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set;

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

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);
    }
    System.out.println(tmpl.render(results));
}

/**
 * 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;
}


/**
 * 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) throws IOException {
    String input = "";
    boolean isValid = false;
    do {
        System.out.printf("Enter new word for %s: ", phrase);
        input = mReader.readLine();
        if(mCensoredWords.contains(input)){
            System.out.printf("%nSorry, %s is a censored word. Please try again.%n", input);
            isValid = true;
        }
    } while(isValid);
    return input;
}

}

I don't know what I did wrong. It run correctly in Intellij. It keep giving me OOM.

com/teamtreehouse/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) {
        // write your code here
        Prompter prompter = new Prompter();
        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);
        prompter.run(tmpl);



    }
}
com/teamtreehouse/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.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


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

    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);
        }
        System.out.println(tmpl.render(results));
    }

    /**
     * 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;
    }


    /**
     * 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) throws IOException, NullPointerException {
        String word = "";
        do {
            System.out.printf("Please enter a(n) %s%n", phrase);
            mReader = new BufferedReader(new InputStreamReader(System.in));
                try { word = mReader.readLine();
                } catch (IOException e) {
                  System.out.println("There was a problem prompting for words");
                  e.printStackTrace();
                } catch (NullPointerException n) {
                    System.out.println("Null is not accepted");
                    n.printStackTrace();
                }
            if (mCensoredWords.contains(word)) {
                System.out.printf("The word %s is censored, please enter a valid %s%n", word, phrase);
            }
            else if (word.isEmpty()) {
                System.out.printf("You did not enter a(n) %s, please try again.%n", phrase);
            }
        } while (mCensoredWords.contains(word) || word.isEmpty());
        return word;
    }
}
pseudo-tests.md
#  This is essentially what I am testing 
1.  The user is prompted for a new string template (the one with the double underscores in it).

  a. The prompter class has a new method that prompts for the story template, and that method is called.

2.  The user is then prompted for each word that has been double underscored.

   a. The answer is checked to see if it is contained in the censored words.
      User is continually prompted until they enter a valid word

3.  The user is presented with the completed story
Doli Harahap
Doli Harahap
6,246 Points

Why the input like this for the testing? is this expected?

prompt1: name

Enter new word for name: input : Testing first prompt and then second prompt prompt1: adjective

Enter new word for adjective: input : PASS_1 prompt1: noun

Enter new word for noun: input : dork

Sorry, dork is a censored word. Please try again.

Enter new word for noun: input : dweeb

Sorry, dweeb is a censored word. Please try again.

Enter new word for noun: input : PASS_2 prompt1: noun

Enter new word for noun: input : null java.lang.NullPointerException at com.teamtreehouse.Prompter.promptForWord(Prompter.java:85) at com.teamtreehouse.Prompter.promptForWords(Prompter.java:63) at com.teamtreehouse.Prompter.run(Prompter.java:42) at com.teamtreehouse.Main.main(Main.java:16) at JavaTester.run(JavaTester.java:77) at JavaTester.main(JavaTester.java:39)

1 Answer

Doli Harahap
Doli Harahap
6,246 Points

OK, ignore my question. The OOM I got is because my do-while enters never-ending loop.

The NullPointer I got, is simply I did not read the psuedo-code carefully. But, I think it can be a feedback to Craig to make the instruction a bit clearer maybe?

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Agree with you there. I did the same mistake on not reading the pseudo code carefully and only focus on TODOs. But look at it this way.... His tricks will also train us to look beyond the obvious hints... At least it keeps me challenged while frustrated in the same time.