0

I don't understand what wrong. I am new in java programming. This is my simple code :

import java.io.Console;
public class TestCode
{
  public static void main(String[] args)
  {
     Console console = System.console();
     console.printf("HELLO WORLD\n");
     System.out.printf("Hello World");
  }
}

I don't understand where the the error is! Please anyone could explain these.

2
  • Are you running this from Eclipse? Take a look at this: stackoverflow.com/a/14439333/1344008 Commented Apr 30, 2015 at 17:10
  • 2
    Is this an exact copy and paste of your code? "SYstem" is capitalized wrong, which should cause a compiler error. I'm wondering if there are other differences. Commented Apr 30, 2015 at 17:13

4 Answers 4

1

The method System.console() can return null if there is no console device present.

Read the post of given (below) link.

Java Syslem.console IDE and Testing

Sign up to request clarification or add additional context in comments.

Comments

0

i guess the Problem is an IDE, does not use the console, so you need to compile it and run it with an terminal or cmd.exe under windows. So if you start that program from an IDE, on the JRE there does not exist an console, because the IDE has no console. For simple output i would use:

System.out.println("something");

Comments

0

Look at the stack trace and see which line you are getting an error at. Better yet, put your code inside try/catch, print the stack trace.

As others have said, System.console() is returning null.

https://docs.oracle.com/javase/tutorial/essential/io/cl.html

Comments

0
import java.io.Console;
public class TestCode
{
  public static void main(String[] args)
  {
     Console console = System.console();
     if(console != null){
     console.printf("HELLO WORLD\n");
     }else{
         System.out.println("HELLO WORLD\n");
     }
     System.out.printf("Hello World");
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.