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 trialdrew s
Python Development Techdegree Graduate 19,491 PointsNot 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
Chris Shearon
Courses Plus Student 17,330 PointsIt'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");
}
drew s
Python Development Techdegree Graduate 19,491 PointsThere is nothing wrong when I run this to treehouse workspaces but if I use VS and run the program I received that error
Lauren Moineau
9,483 PointsYou 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
Python Development Techdegree Graduate 19,491 PointsI tried using scanner it works a little but its not functional compared to treehouse workspaces. Do you know any good IDE for Java?
Lauren Moineau
9,483 PointsYou 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.
Chris Shearon
Courses Plus Student 17,330 PointsThere's several perfectly good choices out there but Intellij is my personal favorite.
Lauren Moineau
9,483 PointsSame here :)
drew s
Python Development Techdegree Graduate 19,491 PointsOk thanks guys I will check that out. So far I switched back to Atom
Lauren Moineau
9,483 PointsOK. You're welcome :)