You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "John Wagenleitner (JIRA)" <ji...@apache.org> on 2016/10/21 00:14:59 UTC

[jira] [Commented] (GROOVY-7244) Cannot Pipe Standard Input to Groovy Script

    [ https://issues.apache.org/jira/browse/GROOVY-7244?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15593448#comment-15593448 ] 

John Wagenleitner commented on GROOVY-7244:
-------------------------------------------

The [javadoc for System#console|https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#console()] states that it can return {{null}} which is what is happening here.  So it's not the piping of stdin that is causing the problem, the problem is that there is no console so the NPE is thrown when {{readLine}} is called.

If you wrap {{System.in}} in a BufferedReader or use the Scanner class (see below) in {{helloname.groovy}} the piping works:

{code:title=helloname.groovy}
#!/usr/bin/env groovy
import java.util.Scanner

name = new Scanner(System.in).nextLine()
printf "Hello %s!\n", name
{code}

{code}
$ echo "John Doe" | ./helloname.groovy
Hello John Doe!
{code}

{code}
$ echo Hello > sometext; ./helloname.groovy < sometext
Hello Hello!
{code}

> Cannot Pipe Standard Input to Groovy Script
> -------------------------------------------
>
>                 Key: GROOVY-7244
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7244
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.3.7
>         Environment: $ echo $(groovy -v) $(sw_vers | grep "ProductVersion" | cut -d$'\t' -f2)
> Groovy Version: 2.3.7 JVM: 1.7.0_60 Vendor: Oracle Corporation OS: Mac OS X 10.8.5
>            Reporter: Joaquin Menchaca
>         Attachments: helloname.groovy
>
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> Piping Standard Input into Groovy Script causes NullPointerException:
> $ echo "John Doe" | ./helloname.groovy 
> Caught: java.lang.NullPointerException: Cannot invoke method readLine() on null object
> java.lang.NullPointerException: Cannot invoke method readLine() on null object
> 	at helloname.run(helloname.groovy:2)
> $ echo Hello > sometext
> $ groovy helloname.groovy < sometext
> Caught: java.lang.NullPointerException: Cannot invoke method readLine() on null object
> java.lang.NullPointerException: Cannot invoke method readLine() on null object
> 	at helloname.run(helloname.groovy:2)
> $ cat helloname.groovy 
> #!/usr/bin/env groovy
> name = System.console().readLine "Enter your name: "
> printf "Hello %s!\n", name



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)