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

java error

Hey guys i just started learning java and made this simple program and i dont know whats wrong with it. this is what the compiler is telling me (i'm using intellij idea): Exception in thread "main" java.lang.NullPointerException at com.treehouse.java.Main.main

this is my code:

package com.treehouse.java;

import java.io.Console;

public class Main {

public static void main(String[] args) {
    Console console = System.console();
    String q = console.readLine("what's your name? ");
    console.printf("Hi there %s , my name is ammar!", q);
}

}

any help is appreciated :)

2 Answers

Common issue with IDE's and java.io.Console. See: https://teamtreehouse.com/community/problem-in-ide

I've already tried the system.out.println and the other ones and its still showing error. should i change my code editer ?

I would suggest changing from Console, to a BufferedReader. It's a little bit more steady, especially with IDEs. It's a little tough to get set up at first, but once you get it, it's pretty easy.

Basically, put this in your code:

private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

then instead of console.readLine, do br.readLine(). It'll complain of an IOException, but if you click on the red lightbulb on the side, it'll surround it with a try/catch block for you, which will fix the error.