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 Java Basics Getting Started with Java Introduction to Your Tools

Aaron Coursolle
Aaron Coursolle
18,014 Points

If you ran this in an IDE, what would the alternative code look like?

As a review, and a way to get used to IntelliJ IDEA, I thought it would be a good idea to review the videos that I've already completed. However, this code won't work in an IDE environment.

I get that console was a shortcut that the instructor used, so that he could get to more important (especially for beginners) concepts. But I'm now at the "details" level and filling in the gaps that I missed my first time through the course.

With that long explanation, here is the code:

import java.io.Console;

public class Introductions {
    public static void main(String[] args)
    {
        Console console = System.console();
        console.printf("Hello There");
    }
}

Here is the error:

Exception in thread "main" java.lang.NullPointerException
    at Introductions.main(Introductions.java:7)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Process finished with exit code 1

What changes would I have to make, temporarily, to make an import console program work in IntelliJ?

Aaron Coursolle
Aaron Coursolle
18,014 Points

I'm not looking for an alternative HelloWorld program but import console alternatives.

1 Answer

First, import java.util.Scanner; then instantiate a Scanner object like this:

Scanner input = new Scanner(System.in);

System.out.println("Enter anthing here >>>  ");
String string = input.next();
System.out.printf("You said %s.", string);