0

I'm on Ubuntu 16.04 and trying to set up Eclipse.

Why does the following code work in terminal but not in Eclipse jee-oxygen? Package helloworld is not included in the code I run in terminal.

package helloworld;
import java.io.*;
public class helloworld
{
    public static void main(String[]args)
    {
        Console in = System.console();
        String input = in.readLine();
        System.out.println(input);
    }
}

error :

Exception in thread "main" java.lang.NullPointerException
at helloworld.helloworld.main(helloworld.java:8)

/etc/environment :

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java"

I've added java-8-openjdk to the build path in Eclipse.

What am I missing out here?

1 Answers1

0

Thee Console-object is null. System.console() only returns the console, if any exist. In Eclipse this is a known issue. But you can use a Scanner instead:

Scanner scanner = new Scanner(System.in);
String input= scanner.nextLine();
Christian
  • 22,585
  • 9
  • 80
  • 106