Skip to main content

System.console().readLine() bug?

2 replies [Last post]
t800t8
Offline
Joined: 2006-03-03
Points: 0

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(DesktopTest.java:38)<br />
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br />
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)<br />
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)<br />
	at java.lang.reflect.Method.invoke(Method.java:597)<br />
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)<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 />
                        desktop.open(file);<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.
mernst
Offline
Joined: 2005-02-19
Points: 0

You fail to qualify what "DesktopTest.java:38" is but I suspect that you're running directly out of the IDE and System.console() is simply null in that case.

http://download.java.net/jdk6/docs/api/java/io/Console.html
"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."

t800t8
Offline
Joined: 2006-03-03
Points: 0

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