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 Basics Getting Started with Java Receiving Input

Prabh Bajwa
seal-mask
.a{fill-rule:evenodd;}techdegree
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points

I'm having this issue with the code which works fine in Workspaces but not in my System's command prompt?

Hello, Dear Sir/Ma'am, I'm having this issue with the Java program it works fine in Workspaces but now in my Command prompt. please help me I really need to make it work in my System as well.

Thanks in Advance and Sorry I'm not that good in English that's why there could be lots of mistakes.

import java.io.Console;

public class Introductions {

    public static void main(String[] args) {
        Console Console = System.Console();
        //Welcome to the Introductions program! Your code goes below here
        String firstName = Console.readLine("What is your name?  ");
        //thisIsAnExmapleOfCamelCasing
        Console.printf("Hello, my name is %s\n", firstName);
        Console.printf("%s is learning how to write Java\n", firstName);
    }
}

I'm trying to make this code run in my System's command prompt.

Error I'm getting is

Introductions.java:6: error: cannot find symbol Console Console = System.Console(); ^ symbol: method Console() location: class System 1 error [Finished in 0.6s]

7 Answers

Hi there,

Two typing errors - change cosole to console. The rest looks OK, I think.

Steve.

I added a comment in your code to highlight where that little issue is.

Prabh Bajwa
seal-mask
.a{fill-rule:evenodd;}techdegree
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points

Hello, Sir thanks for the answer as I corrected the Typo but still it's not running in my System's command prompt. I hope you can help sir.

Hi again,

Class names are capitalised - so change:

console console = System.console();

to

Console console = System.console();

That should fix it - let me know if it doesn't.

Steve.

The import needs cpaitalising too:

import java.io.Console;

However, you need to make sure you are creating a console application else this won't work on your machine.

You now have:

Console Console = ...

Change to:

Console console = ...

I'm assuming this is a Windows machine?

In your command line make sure that java -version gives you output similar to this:

java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

If it does, then creating a file called Introductions.java (make sure it isn't a .txt file) in that folder should work the same way as your Workspace. You should be able to type java Introductions.java && java Introductions which should run your program. I can confirm your code works here with no problem.

Steve.

This code works fine:

Introductions.java
import java.io.Console;

public class Introductions{

    public static void main(String[] args) {
    // write your code here
        Console console = System.console();
        //Welcome to the Introductions program! Your code goes below here
        String firstName = console.readLine("What is your name?  ");
        //thisIsAnExmapleOfCamelCasing
        console.printf("Hello, my name is %s\n", firstName);
        console.printf("%s is learning how to write Java\n", firstName);
    }
}
Prabh Bajwa
seal-mask
.a{fill-rule:evenodd;}techdegree
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points

now I'm getting this error Exception in thread "main" java.lang.NullPointerException at Introductions.main(Introductions.java:9) [Finished in 0.7s]

Did your code run at all, or just throw the error?

Prabh Bajwa
seal-mask
.a{fill-rule:evenodd;}techdegree
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points

Yes, I'm using windows computer and this is my Java -version java version "1.8.0_162" Java(TM) SE Runtime Environment (build 1.8.0_162-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

So Java will work in that folder (and probably all others) - so there's a problem with your code. Can you post your current code, please?

Prabh Bajwa
seal-mask
.a{fill-rule:evenodd;}techdegree
Prabh Bajwa
Front End Web Development Techdegree Student 1,665 Points
import java.io.Console;

public class Introductions{

    public static void main(String[] args) {
    // write your code here
        Console console = System.console();
        //Welcome to the Introductions program! Your code goes below here
        String firstName = console.readLine("What is your name?  ");
        //thisIsAnExmapleOfCamelCasing
        console.printf("Hello, my name is %s\n", firstName);
        console.printf("%s is learning how to write Java\n", firstName);
    }
}

I can't see any reason for a NullPointerException in there? It seems to be as you access the console with readLine - but I can't see that there's anything wrong with your code.

This code definitely works with no exceptions - copy it into your file and see whether it works for you:

import java.io.Console;

public class Introductions{

    public static void main(String[] args) {
        Console console = System.console();
        String firstName = console.readLine("%nWhat is your name?: ");
        console.printf("%nHello, my name is %s.%n%n", firstName);
        console.printf("%s is learning how to write Java.%n", firstName);
    }
}

Is your file called Introductions.java?