Skip to main content

System.console().readLine() bug?

2 replies [Last post]
Joined: 2006-03-03

When i try to run below code on Ubuntu 6.06 with JDK 6 build 96 in IntelliJ IDEA 6 build 5622, I have this exception

<br />
Exception in thread "main" java.lang.NullPointerException<br />
	at com.apress.java6PlatformRevealed.chapter01.DesktopTest.main(<br />
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br />
	at sun.reflect.NativeMethodAccessorImpl.invoke(<br />
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(<br />
	at java.lang.reflect.Method.invoke(<br />
	at com.intellij.rt.execution.application.AppMain.main(<br />

        File dir = new File(path);<br />
        File[] files = dir.listFiles();</p>
<p>        for (File file : files) {<br />
            System.out.println("Open " + file.getName() + "? [YES/NO] : ");</p>
<p>            if (desktop.isSupported(Desktop.Action.OPEN)) {<br />
                String line = System.console().readLine();</p>
<p>                if ("YES".equals(line)) {<br />
                    System.out.println("Opening... " + file.getName());</p>
<p>                    try {<br />
              ;<br />
                    } catch (IOException e) {<br />
                        System.err.println("Unable to open: " + file.getName());<br />
                    }<br />
                }<br />
            }<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-02-19

You fail to qualify what "" is but I suspect that you're running directly out of the IDE and System.console() is simply null in that case.
"Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console."

Joined: 2006-03-03

Correct. I tried to run in terminal and it works. Thanks