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.

  • Are you running this from Eclipse? Take a look at this: http://stackoverflow.com/a/14439333/1344008 – npe Apr 30 '15 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. – Andrew Janke Apr 30 '15 at 17:13

4 Answers4

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

Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
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");
Bierbarbar
  • 1,399
  • 15
  • 35
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

J.J
  • 633
  • 1
  • 6
  • 14
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");
  }
}
Alaa Abuzaghleh
  • 1,023
  • 6
  • 11