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

Java

I am getting an "Exception"

I am getting this error, when I trying run my application :( Exception in thread "main" java.lang.NullPointerException

'import java.io.Console;

public class Story { 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 Code goes here... */ Sting name = console.readLine("Enter your name: "); String adjective = console.readLine("Enter an adjective: ");

    console.printf("%s is very %s\n", name, adjective);
}

}'

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

I formatted you code for better reading:

import java.io.Console;

public class Story { 

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 Code goes here... */

Sting name = console.readLine("Enter your name: "); 
String adjective = console.readLine("Enter an adjective: ");

console.printf("%s is very %s\n", name, adjective);
}

Where do you run this code? In the workspace?

3 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Bruice,

when I run this code in the workspace everything is fine.

import java.io.Console;

public class Main { 

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 Code goes here... */

String name = console.readLine("Enter your name: "); 
String adjective = console.readLine("Enter an adjective: ");

console.printf("%s is very %s\n", name, adjective);
    }
}

The commands:

javac Main.java

and then

java Main

Let us know if you could solve the issue ...

Grigorij

I ran this code on "IntelliJ idea". Which is on my local computer, but it works on "workspace". I am using JDK 1.7.0_79 JRE 1.7.0_79. I couldn't figure out the problem. it compile without no error, but when I try to execute the program it's spit this exception, I searched for this error in Google, lot's of people had the same error but when see the solution, it looks daunting.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Its because you use the console class ... Use scanner instead .. Scanner will not throw the exception in your case

And report if you have been successful !

Thank you so much. I got it, it work with command line :) thanks

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey,

you are very welcome !

Keep on doing the good job :thumbsup:

Yeah! I am using Scanner to get input. :) But I have to understand that Exception. I am enough to know Java yet but I will figure out why that Exception raised. And thanks for your reply :)

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

HeyBruice,

If you running your program from an IDE the console.readLine can return null and cause the java.lang.NullPointerException.

So if we look at your code, the console.readLine() should store a String representation of the User input. And print it using printf method. The printf method expects an String object (input from the user) but gets a null.

So the java.lang.NullPointerException is thrown when an application attempts to use null in a case where an object is required.

The reason because System.console() returns a null is that an IDE like IntellyJ or Eclipse are not using console.

Otherwise If you try invoking your program from the command line using the java command then it will have a console and the method should not return null.

I have found this text snippet:

"Whether a programm environment 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"

I hope I could help you clear the issue a little bit :smiley:

Grigorij