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

Angel Brambila
Angel Brambila
5,847 Points

Throw Null Pointer when i use the Console

My Main.java is :

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__.";
        Console console = System.console();
        String story = console.readLine();
        Template template = new Template(story);
        prompter.run(template);


}

}

its says Null pointer when i use String story = console.readLine();

if i uncomment the other String story when i local run it it works, but when i put in in the challenge i see that here they use inly 2 params, then i use the console to get the Story template but gives me this Null pointer Exception X_X.

i don't know what to do. :S

com/teamtreehouse/Main.java
package com.teamtreehouse;

import java.io.Console;
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();
      Console console = System.console();
        //String story = "Thanks __name__ for helping me out.  You are really a __adjective__ __noun__ and I owe you a __noun__.";
      String story = console.readLine();
      Template template = new Template(story);
        prompter.run(template);

    }
}
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 template) {
        List<String> results = null;
        try {
            results = promptForWords(template);
        } catch (IOException e) {
            System.out.println("There was a problem prompting for words");
            e.printStackTrace();
            System.exit(0);
        }
        System.out.printf("Your TreeStory:%n%n%s", results);
        String tree = template.render(results);
        System.out.printf(tree);
    }

    /**
     * Prompts user for each of the blanks
     *
     * @param template The compiled template
     * @return
     * @throws IOException
     */
    public List<String> promptForWords(Template template) throws IOException {
        List<String> words = new ArrayList<String>();
        for (String phrase : template.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 {
        boolean isInvalidWord;
        do {
            phrase = mReader.readLine();
          System.out.printf("String is %s%n",phrase);
            isInvalidWord = phrase.equalsIgnoreCase("dork") ||
                    phrase.equalsIgnoreCase("jerk") ||
                    phrase.equalsIgnoreCase("nerd") ||
                    phrase.equalsIgnoreCase("dweeb") ||
                    phrase.equalsIgnoreCase("stupid") ||
                    phrase.equalsIgnoreCase("asshole");

            if(isInvalidWord){
                System.out.printf("Language not Allowed. Please Try Again%n");
            }
        }while(isInvalidWord);
        return phrase;
    }
}
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
Angel Brambila
Angel Brambila
5,847 Points

I know that console.readLine() returns a string, but Console give you the object of the console of the java virtual machine (that says the documentation), but i still cant know how to fix it.

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hey Angel!

Can you try putting a new method inside the Prompter class and use the reader provided there already. I think you might be running into issues creating a new one.

Let me know if that helps!

Angel Brambila
Angel Brambila
5,847 Points

It works, but now i have an error in the challenge, the challenge says : "you're not censoring "dork", but when i debug in local environment and i "debug"(hardcore way) in the console of the challenge i see this.

Enter tour story:

The Story was: Testing first prompt and then second prompt Enter a Word: Word is PASS_1 Enter a Word: Word is dork Language not Allowed. Please Try Again Enter a Word: Word is dweeb Language not Allowed. Please Try Again Enter a Word: Word is PASS_2 Your TreeStory:

[PASS_1, PASS_2] Testing PASS_1 and then PASS_2

(i can't upload an image to show you how, so i paste the console preview).

When i debug in local environment it works, when i put 1 of the censored words:

public String promptForWord(String phrase) throws IOException { boolean isInvalidWord; do { System.out.printf("Enter a Word: %n"); phrase = mReader.readLine(); System.out.printf("Word is %s%n",phrase); isInvalidWord = phrase.equalsIgnoreCase("dork") || phrase.equalsIgnoreCase("jerk") || phrase.equalsIgnoreCase("nerd") || phrase.equalsIgnoreCase("dweeb") || phrase.equalsIgnoreCase("stupid") || phrase.equalsIgnoreCase("asshole");

        if(isInvalidWord){
            System.out.printf("Language not Allowed. Please Try Again%n");
        }
    }while(isInvalidWord);
    return phrase;

isInvalidWords gets true, and still looping.

x_X.

Craig Dennis
Craig Dennis
Treehouse Teacher

I loaded up a java.util.Set for you to use called mCensoredWords. But I think the problem is in fact that you are printing out the word that I didn't want you to use ;). I'm checking the output for that word. Try removing your debugging.

Looking good! You are super close!

Angel Brambila
Angel Brambila
5,847 Points

OMG , finally XD!!!!, im suuuuper happy :D!!!!!!!!!! (a lot of !) :). Thanks Craig, i solve it making an auxiliary before the Prompt of the word that way when they put a censored word still asking for name , noun, adjetive etc. :D!!!.

Still hungry for more java stuff :).

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Angel,

IDE's actually can't integrate console easily. It involves how they wrap the objects and with console being final as far as I know.

Your best way bet is to learn one of the below classes for input/output

Scanner

BufferedReader

Once you start coding in an IDE you'll quickly come to realize that unfortunately Console get's outgrown very fast.

Angel Brambila
Angel Brambila
5,847 Points

I actually put it with scanner, but it isn`t working, i'll try now with the buffered Reader :), thnks Rob.