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
Israel Herrera
429 PointsI am getting an exception in my IDE when trying to print a simple line through console.printf();
Exception in thread "main" java.lang.NullPointerException at Words.main(Words.java:7)
is the error message I get when I run the code below. Can someone shed some light on what I am doing wrong? :) I have tried this both in Intellij and Grooy Grails Tools Suite.
import java.io.Console; public class Words {
public static void main(String[] args) {
Console console = System.console();
console.printf("this is a test");
}
}
Israel Herrera
429 PointsI will give that a shot.
1 Answer
Will Scharff
11,815 PointsYou could have the system write to standard out (stdout) and it would print to console this way. Plus it would not need an overhead for the import of java.io.Console.
For example just have this in main:
System.out.print("This is a test");
However, if you do want to make sure you are using Console.printf(), from what I read of the reference/manual page, you have not entered any information into the console and it is null. For example there is a method called "readLine." You could do:
String test = console.readLine(Enter String: ):
Once there is a string entered, then the console.printf should not be a null pointer and will not cause the null pointer exception. I hope this helps!!
syalih
16,468 Pointssyalih
16,468 PointsCode is Ok did you try on Workspaces its working maybe some problem in your IDE dont know... :)