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 final TreeStory challenge

My code works in IntelliJ. When I submit it, I keep getting weird NullPointerExceptions. I have tried handing my code empty strings and such to try and cause an NPE, but I just can't replicate it on my machine. Is there a bug in the tests? I am getting pretty frustrated. This is the first time since starting TreeHouse where I wasn't able to at least finish the recommended steps for the day.

7 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Sorry Patrick...just got back to this....

I was expecting this to be added to the Prompter and to use the existing reader. Something like this:

Prompter.java
    // ...
    public String promptForStoryTemplate() {
        System.out.printf("Enter the story:  ");
        String line = null;
        try {
            line = mReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return line;
    }
   //...

That said, however, your example of creating a new reader should work, something in my code is breaking it, very sorry about that and the added frustration.

Tim Strand
Tim Strand
22,458 Points

I dont understand how this knows where to insert the name etc? This is the piece i am falling down on. It works fine with the intial fed story but when i try to prompt for a user story i dont understand how to insert the prompted words into that story.

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey, it's always a good idea to post the code that is causing you the trouble. Not sure I can suggest help without knowing what exactly you are doing.

I guess I was just wondering if anybody else was having this issue, or if Craig could provide insight into what the tests for the code are, as my code does everything in the spec and doesn't give any errors while running on my computer.

I'd rather figure it out myself, but I didn't want to be banging my head against a wall if there was a problem on the server-side.

Ryan Ruscett
Ryan Ruscett
23,309 Points

Ooh ok, I made it through and everything worked. So I didn't have any problems. But it can be confusing considering so much happens behind the scenes during the exercises.

Ok, thanks for that.

I will re-write it a third time from scratch, I guess.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hey Patrick!

As you can probably imagine, its pretty hard to write a blind functional test ;). So sorry in advance, I imagine you introduced an edge case that I missed.

Follow those todos and pseudo tests as close as possible. Did you add any additional functionality?

It expects the prompt for the new story, continuous prompts for valid responses and then expects the printout

Did you add an additional prompt?

Hope that helps, and keep me posted on what it was so I can attempt to fix the test.

Thanks!

Hi,

As far as I can tell, I've done all the TODOS, fulfilled all the requirements, and not added anything extra.

I've rewritten it for a third time. The output I get in the preview window is:

Enter a first prompt: Enter a second prompt: Your TreeStory:

Testing null and then null

The error message says

Looks like you didn't present the TreeStory (3)

BUT it totally does print on my machine.

I thought maybe this meant that I needed to handle the cases where you enter an empty string instead of a word, but when I check for that, I get the NullPointerException instead.

When I run the version of the code that doesn't require an actual word on my machine, there are no errors when entering an empty string,

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Weird your output is what I am expecting...minus the nulls....so it looks like the first prompt must be doing something to my test input. Can you show me how you are prompting for the template please?

package com.teamtreehouse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
    // write your code here
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String story = null;
        try {
            System.out.println("Enter template: ");
            story = br.readLine();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.exit(1);
        }
        Prompter prompter = new Prompter();
        Template tmpl = new Template(story);
        prompter.run(tmpl);
    }
}
Craig Dennis
Craig Dennis
Treehouse Teacher

On my phone at the moment, so I can't test...can you try doing that BuffereReader in a try-with-resources block. I think its because you have 2 readers open on System.in. The try with resources auto closes the reader.

Sorry for the confusing error message!

Fingers crossed that works!

That causes the program to throw an IOException on my system, but in the Prompter class. It accepts the template, but then dies in the run method when calling promptForWord();

The output I get on Treehouse is:

JavaTester.java:77: error: unreported exception IOException; must be caught or declared to be thrown
    Main.main(null);
             ^
1 error

Thanks! It makes sense to put that method in Prompter, I misinterpreted the TODOs in the main method. It passes the tests now.