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 trialJohn Paige
7,436 PointsWorks in Workspaces, but not in IntelliJ IDE
Hey there. I got my code to function as it should in Workspaces. Initally, I wanted to make it work in an IDE (IntelliJ). It didn't work there, but it did function correctly in TreeHouse workspaces. The error that pops when i compile is this:
Exception in thread "main" java.lang.NullPointerException
at Greeter.main(Greeter.java:11)
Process finished with exit code 1
I appreciate any input!
2 Answers
Dane Parchment
Treehouse Moderator 11,077 PointsShow us your code so that we can properly debug for you!
These could be potential problems/fixes:
- Check and see if you have imported all the required classes that you need to use in order to have this Greeter class work.
- Make sure you are calling the proper variable names and didn't misspell whatever you were calling or referring to on line 11.
- Check if you manually set a variable to null, sometimes if you set a variable to null (especially an array). Calling it later or could cause an error since it isn't technically instantiated.
Dane Parchment
Treehouse Moderator 11,077 PointsOk I see now! So I believe the problem is:
You need to check for errors, do a try-catch on all the code within the main method, and check for whether the variable console is null or not! I believe that the reason console can throw null depends on how it is called. The documentation states that the console will return null when it reaches the EOF (end of file).
Because the console returns null at the end of the stream/file it is possible that console is not being populated with the actual console. First try running the program through the console itself:
java Greeter.java
, and see if that works instead of running it through your IDE.If running it in the console works, then you have solved the problem. Otherwise you should utilize
Scanner
instead to get the user's input, as your system is having problems populating the Console class with an actual console.
John Paige
7,436 PointsJohn Paige
7,436 PointsMy workspace: https://w.trhou.se/mw1wm43fwg
My IntelliJ project: