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 trialtylerpostuma2
5,175 Pointsjava.lang.NullPointerException Error
Hey everybody. I was working on a madlibs type project outside of workspaces to keep testing my java knowledge. One problem that I ran into was this java.lang.NullPointerException error. It keeps telling me that there is an error on line 10 of my code, but I really don't see an error. Any help would be great! Thanks!
2 Answers
Alison Brodribb
24,615 PointsAre you trying this out on Netbeans on your computer?
Netbeans doesn't support Console.readLine - I ran into this problem ages ago when I was doing some of my own code, and eventually just decided to use another application, since the issue looks like it dates back to 2008 and doesn't seem to be fixed yet.
To check your code, I'd either try it in workspaces (FYR, when I tester your code in workspaces, it worked fine), or set up a Java Development on your computer.
This website gives pretty clear instructions for installing a JDE on your computer
http://www.tutorialspoint.com/java/java_environment_setup.htm
Once you've installed Java and added it to your path, you can edit your data in any editor you choose (I use atom https://atom.io/ but you can use whatever you prefer).
To compile/run your code, just open up your console, go to the directory where you've saved your main class, and run as you've learned in the tutorials
tylerpostuma2
5,175 PointsRight now, I'm using eclipse IDE for my computer. I have tried setting up another program just to see if it would work and I still got the same error.. I looked it up on google and it said that it was trying to grab the value of the variable, but it's a null. I'm not really sure what a null is, or how to fix it. Any help with that would be awesome, thanks.
Alison Brodribb
24,615 PointsThe reason you're getting the NPE is that System.Console() returns null in IDEs.
Null is basically a "no value entered" (as opposed to 0, which is an integer of value zero).
When I was having my issues with Netbeans, I recall that I was advised that Eclipse had similar issues, which is why I went and moved over to the console as I advised above. I have a solution that should work with Eclipse below, however if you want to get it to work using console.readLine, I would recommend using a comnination editor/command line environment as suggested above.
Stack overflow has a solution that I've linked below, which bypasses the need for the console.ReadLine method, which is causing your issue. I tested it on Netbeans and it worked there, so it should work for Eclipse. The code used in this fix is stuff that's addressed in the Java Data Structures course (not the main focus of the course, but is shown there).
http://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console
Below is what your code should look like if you use the code in the above example.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("How Old are you? ");
String ageString = br.readLine();
int age = Integer.parseInt(ageString);
if (age > 75) {
System.out.print("You are too old to play this game.");
} else {
System.out.print("Please enter a noun");
String noun = br.readLine();
System.out.print("Please enter an adjective");
String adjective = br.readLine();
}
System.out.print("Your story");
System.out.print("--------------");
} }
tylerpostuma2
5,175 PointsOk, i'll try that out! Thanks!
tylerpostuma2
5,175 Pointstylerpostuma2
5,175 PointsHere is a link to the picture of the code:
http://imgur.com/obZ1s5Z