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

drew s
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
drew s
Python Development Techdegree Graduate 19,491 Points

Not running

import java.io.Console;

public class Introductions {

    public static void main(String[] args) {
        Console console = System.console();
        // Welcome to the Introductions program!  Your code goes below here
        console.printf("Hello, my name is Felix");
  }
}

I am getting this Error:

Exception in thread "main" java.lang.NullPointerException
    at Introductions.main(Introductions.java:18)

5 Answers

It's possible for the console object to be null. NullPointerException can be thrown when calling a method on a null object. You can do a simple test for null.

Console console = System.console();
        if (console != null) {
            //safe to call method
            console.printf("console object is not null");
        }else {
            System.out.println("console object is null");
        }

You might want to use Scanner instead of Console in VS. I know some IDEs react with a NPE when using Console. Try:

import java.util.Scanner;

public class Introductions {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // Welcome to the Introductions program!  Your code goes below here
        System.out.printf("Hello, my name is Felix");
  }
}

Hope that helps :)

drew s
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
drew s
Python Development Techdegree Graduate 19,491 Points

I tried using scanner it works a little but its not functional compared to treehouse workspaces. Do you know any good IDE for Java?

You can try IntelliJ. The Community edition is free. I'm not familiar with Visual Studio, but I've never had any problem with Scanner on IntelliJ.

There's several perfectly good choices out there but Intellij is my personal favorite.

Same here :)

OK. You're welcome :)