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
Chadley Lyell
1,159 PointsJava Problems
I am having some trouble with running java… I am not allowed to use terminal because this is a school issued laptop, but when I go to an online website that will compile and execute my code it keeps giving me errors. But when I use workspaces it works just fine I used the java starter code Introductions.java, I copied it into the website and tried doing
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
String firstName = "Chadley";
console.printf("Hello! My name is %s\n", firstName);
} }
As you can see I do not believe that I am doing anything wrong but it is giving me this error,
Exception in thread "main" java.lang.NullPointerException at Introductions.main(Introductions.java:9)
The websites I am trying to use are http://www.browxy.com/ https://www.compilejava.net/
But for some reason when I use this website with the code it works just fine! https://www.tutorialspoint.com/compile_java_online.php
Can someone please help me out?
1 Answer
Livia Galeazzi
Java Web Development Techdegree Graduate 21,083 PointsHave a look here:
http://docs.oracle.com/javase/7/docs/api/java/io/Console.html
"Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.
If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null. "
When the JVM has no console the method returns null and you get a NullPointerException.
Juthawong Naisanguansee
Full Stack JavaScript Techdegree Graduate 80,724 PointsJuthawong Naisanguansee
Full Stack JavaScript Techdegree Graduate 80,724 Points+1 Great Answer :)
Nils Garland
18,416 PointsNils Garland
18,416 PointsWell said, all info is here Chadley.