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 trialRaj Gill
Courses Plus Student 560 PointsError in Ecplise with this same code...? WHY?!
/******************************************************************
I keep getting this error in Eciplse:
Exception in thread "main" java.lang.NullPointerException at TreeStory.main(TreeStory.java:14)
*******************************************************************/
import java.io.Console;
public class TreeStory {
public static void main(String[] args) {
Console console = System.console();
/* Some terms:
noun - Person, place or thing
verb - An action
adjective - A description used to modify or describe a noun
Enter your amazing code here!
*/
String name = console.readLine("Enter a name: ");
String adjective = console.readLine("Enter an adjective: ");
String noun = console.readLine("Enter a noun: ");
String adverb = console.readLine("Enter an adverb: ");
String verb = console.readLine("Enter a verb ending with -ing: ");
console.printf("\nYour TreeStory: \n---------------\n");
console.printf("%s is a %s %s. ", name, adjective, noun);
console.printf("They are always %s %s.\n", adverb, verb);
}
}
5 Answers
Craig Dennis
Treehouse TeacherIf you kick off the program from your local machine it will work as intended. Eclipse does a bad job of implementing the java.io.Console
class. So if you navigate to the directory and run the code using the java command it will work properly.
This code was intended to run from the command line and I haven't yet introduced the concept of System.out
and System.in
. If you want to do a readLine
style command you can build a reader like this:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is your first name? ");
String firstName = reader.readLine();
Ken Alger
Treehouse TeacherRaj;
Eclipse does not always have the console option turned on. If you look in the Run/Debug > Console menu, there may be an option to turn it on. Personally I don't ever seem to have much luck with the console in either Eclipse or IntelliJ directly. Your mileage may vary.
Ken
Ilja Torikka
2,178 PointsOr use Scanner like this:
Scanner input = new Scanner(System.in); System.out.println("whats your name: "); String name = input.nextLine(); System.out.println("your name is: " + name);
Sahil Dhawan
1,536 PointsThe reason is that console.printf returns null in eclipse . try using System.out.printf for formatted output and Scanner.nextLine() for input !
Waldo Alvarado
16,322 PointsI was getting the same error on both Eclipse and IntelliJ, I was about to blow my brains out trying to figure out why I was getting a NullPointer then when I tried running the app through my Command line but my javac was "not recognized as an internal or external command" turns out in your Environment Variables, Path, under System variables, you have to type in where your javac lives C:\Program Files\Java\jdk1.7.0_79\bin and make sure to include the bin at the end!
Once I was able to run the file through javac on the Command line it ran fine as it does in the Workspaces.
Raj Gill
Courses Plus Student 560 PointsRaj Gill
Courses Plus Student 560 Points@CraigDennis THANKS! it worked in terminal on my macbook.