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
Jason Holt
10,041 PointsWorking in Eclipse -- Error on first lesson.
Trying to use Eclipse. Here is my code:
import java.io.Console;
public class Introduction {
public static void main(String[] args) {
Console console = System.console();
console.printf("Hello");
}
}
However, I get the following error: Exception in thread "main" java.lang.NullPointerException at teamTreehouse.Introduction.main(Introduction.java:9)
Any help?
3 Answers
Rui Bi
29,853 PointsIf you want to simulate the TreeHouse console in Eclipse, you would do so like this:
import java.io.*;
public class Introduction {
public static void main(String[] args){
PrintStream console = new PrintStream(System.out);
console.printf("Hello");
}
}
The Java.io package provides a class known as Printstream, which is used for output. When you create a Printstream, the constructor can take an argument specifying where you want the output to appear. By assigning it System.out, you are telling it to direct output to the console. There is a Console object in the Java.io package as well, but the default Console object only has the printf() method, not the print() method. You can also use Printstream to write to a file. You can create a file object, and pass it to the Printstream. Then all output would be directed to that file.
Jason Holt
10,041 Pointsdoes the java.io.* import all classes in Java IO?
Craig Dennis
Treehouse TeacherRemember that we are going to use the readLine method on the java.io.Console class as well.
The best solution I can come up with right now is to run this from your local machine using the java command, that will setup the input and output correctly for you. It looks like this is a "bug" in Eclipse and some other IDEs.
There used to be a way to kick off your own process when you press the run button, this would be ideal.
Or you could just use Workspaces for a bit, before we move into Eclipse and IntelliJ ;)
Kai Aldag
Courses Plus Student 13,560 Pointshey Jason,
Your right there, i'm doing AP Com sci and they use eclipse to teach java too. what what to try is,
public class Introduction {
public static void main(String[] args) {
System.out.print("some text here");
}
}
that's what I use in my other course and should work find for you too.
hope this helps, Kai
Jason Holt
10,041 PointsI've used that command as well before. Is there no way to get the console command to work in eclipse? Why would it be giving me an error?
Craig Dennis
Treehouse TeacherHi Jason Holt ,
I chose to use the java.io.Console to not introduce more confusion of using Scanner and System.out and System.in. While not extremely difficult concepts to grok, they add to the overwhelmingness of just starting out and diving into Java.
I used Workspaces to build a console based application, and since we are running it from the command line, it just works. We used Workspaces to get everyone up and running immediately and we do plan to work our way into IDEs, both Eclipse and IntelliJ.
Here is the "bug" about using console in Eclipse. And here is a workaround for IntelliJ.
I will continue to look for a solution to the Eclipse issue you are encountering, but it looks to have been dropped. I do know that if you kick it off using your own JDK it will work.
Hope it helps, I know it wasn't the answer you were looking to hear.
Good news is that I do not think you will need the IDE for the course and people are definitely completing it using Workspaces with very few problems.
Jason Holt
10,041 PointsThanks for the explanation. I totally agree with your method. I was curious just for myself, as I am not a complete beginner. Thanks for your help!
Rui Bi
29,853 PointsRui Bi
29,853 PointsWhen you use an import Java.io., you are telling Java to import all classes from the entire Java.io., package. You could also just import the PrintStream object.
However like Dennis mentioned, later you will need to use the readLine() method from the console as well in order to take in user input. The PrintStream class does not have any methods for taking in input.
A way to practice on Eclipse I supposed would be to create both a PrintStream and a Console object if you need to both take input and produce output.
You could create both objects like this:
Then you would have access to the following methods:
Or the alternative is to only use the console, and limit output to using solely the printf() method.
Then your options are as follows:
The alternative Scanner class that Dennis mentioned is a little more complicated than the Console, and does not have a simple and intuitive readLine() method, which is probably why the decision was made to use the Console instead of a Scanner.