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 tried what I have learned in eclipse..but what is wrong with this! help!

package hihi;

import java.io.Console;

public class practise {

public static void main(String[] args) {

    Console console = System.console();
    String noun = console.readLine("Enter your name: ");
    console.printf("%s is my name", noun);

}

}

this is my coding and in the console I got:

Exception in thread "main" java.lang.NullPointerException at hihi.practise.main(practise.java:10)

can you tell me how can I fix it? is console not working in eclipse??

1 Answer

So your error is on line 10 which is this line in your code

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

What I would do instead of that.

        String name;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter your name: ");
        name = s.nextLine();
        System.out.printf("%s is my name", name);

I am pretty sure because you are setting it to a line that isn't there that is why you are getting a nullPointer because the response is null

and this is the response I got

Enter your name: 
Jordan
Jordan is my name

From my personal experience scanner is overall a lot nice to use when asking for user input scanner has a lot more methods on it. Here is a link for you :) https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html