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 The Solution

John Paige
John Paige
7,436 Points

Works in Workspaces, but not in IntelliJ IDE

Hey there. I got my code to function as it should in Workspaces. Initally, I wanted to make it work in an IDE (IntelliJ). It didn't work there, but it did function correctly in TreeHouse workspaces. The error that pops when i compile is this:

Exception in thread "main" java.lang.NullPointerException
    at Greeter.main(Greeter.java:11)

Process finished with exit code 1

I appreciate any input!

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

Show us your code so that we can properly debug for you!

These could be potential problems/fixes:

  • Check and see if you have imported all the required classes that you need to use in order to have this Greeter class work.
  • Make sure you are calling the proper variable names and didn't misspell whatever you were calling or referring to on line 11.
  • Check if you manually set a variable to null, sometimes if you set a variable to null (especially an array). Calling it later or could cause an error since it isn't technically instantiated.
John Paige
John Paige
7,436 Points

My workspace: https://w.trhou.se/mw1wm43fwg

My IntelliJ project:

import java.io.Console;

public class Greeter {

    public static void main(String[] args) {
        Console console = System.console();
        // TODO:  1. Create a new String named place and assign it a value of your choosing.
        String place = "The Yoga House";
        // TODO:  2. Using the provided console object,
        //           prompt the user "What is your first name?" and store that in a variable
        String firstName = console.readLine("What is your name? ");

        // TODO:  3. Print out to the console "Hello <FIRST_NAME>!  Welcome to <PLACE>."
        System.out.printf("Hello %s! welcome to %s%n", firstName, place);
    }
}
Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

Ok I see now! So I believe the problem is:

  1. You need to check for errors, do a try-catch on all the code within the main method, and check for whether the variable console is null or not! I believe that the reason console can throw null depends on how it is called. The documentation states that the console will return null when it reaches the EOF (end of file).

  2. Because the console returns null at the end of the stream/file it is possible that console is not being populated with the actual console. First try running the program through the console itself: java Greeter.java, and see if that works instead of running it through your IDE.

  3. If running it in the console works, then you have solved the problem. Otherwise you should utilize Scanner instead to get the user's input, as your system is having problems populating the Console class with an actual console.