Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Can NetBeans 8.0 be used to perform the exercises in the Java Track? I tried the first exercise. No go.

I tried using NetBeans 8.0 to follow along with the Java Track. I entered the exercise using NetBeans. It would not compile. I then entered the exercise in Workspaces and it compiled and ran fine. That leads me to believe that Workspaces is doing something different behind the scenes than Netbeans is. If I can't use NetBeans that is OK. I just want to know before I waste a lot of time trying.

I am including the code and the compile error just in case it is something simple I can do to NetBeans to make it compatible.

/*
 * Treehouse.com - Java Basics
 *
 *      This is the INTRODUCTIONS program
 */
package introductions;
import java.io.Console;
/**
 *
 * @author westcoasthawk
 */
public class Introductions {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Console console = System.console();
        console.printf("Hello, my name is Doug");

    }

}

compile error

run: Exception in thread "main" java.lang.NullPointerException at introductions.Introductions.main(Introductions.java:19) /Users/westcoasthawk/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

3 Answers

Seth Kroger
Seth Kroger
56,403 Points

IDEs like NetBeans have issues with java.io.Console when you use their built-in console. You need to use System.in and System.out instead. System.out works virtually the same as Console for output. You just replace console with System.out. When you get to input you need to open a BufferedReader first:

import java.io.BufferedReader;
import java.io.InputStreamReader;

// ...

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.printf("Make a guess: ");
String guess = bufferedReader.readLine();

Also, Console should work if you use a separate command console. It's just the ones inside the IDE that have the issue.

https://teamtreehouse.com/community/java-ide-question has more info.

Seth how would you code "console.readLine("What's your Name") in NetBeans and have it work the same System.in?

Seth Kroger
Seth Kroger
56,403 Points

Since console.readLine(String) prints out the string you pass in as a prompt before accepting input. It's the same as printing the string out with printf before calling readLine without an argument, just with less code. Without console you you use:

System.out.printf("What's your name? ");
bufferedReader.readLine();

Seth: Thanks for the input. I was wondering about that. I have taken one other Java course where we used NetBeans and, as you say, we used System.in and System.out. I appreciate you taking the time to answer.

Geovanie Alvarez
Geovanie Alvarez
21,500 Points

You can also use Scanner for the input

import java.util.Scanner;
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("Make a guess: ");
    String guess = sc.nextLine();
    System.out.println(guess);
}

Also a very good answer.