Results 1 to 4 of 4
  1. #1
    gaguevaras is offline Member
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Default Console input issue

    Hi, I'm writing something that uses input from the console, I'm using SE6.

    My problem code is the following:
    Java Code:
    		Console c = System.console();
    		String number = c.readLine("Number? ");
    The problem is that when I compile using the terminal it works as expected and the 'readLine' prompts the user.

    When I run it using Eclipse IDE it produces a NullPointerException on the first line. The project has the same java compiler as what I'm using in the terminal.

    What could be wrong?

    Thanks.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    What could be wrong?

    Running it in Eclipse.

    As the Console API docs state: "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 this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null."

    Perhaps Eclipse offers some way of running the program outside it's own "Console" environment. (But in that case you would lose the Goodness of being able to click on lines in runtime exceptions etc.)

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    I recommend using a Scanner like so:

    Java Code:
    Scanner s=new Scanner(System.in);

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    There may be a problem with Eclipse and the Console class. Per Console API:

    "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.

    If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null."

    So if you check for a Console object and it doesn't exist, if it is null, you can't use it.

Similar Threads

  1. How to get input from Console
    By karma in forum New To Java
    Replies: 8
    Last Post: 08-13-2010, 10:32 PM
  2. Console Input Issue
    By Java_Lover in forum New To Java
    Replies: 9
    Last Post: 04-22-2010, 10:31 PM
  3. how to take input from console in jsp
    By veena in forum New To Java
    Replies: 1
    Last Post: 05-06-2008, 05:39 AM
  4. ways to get input from console
    By soni in forum New To Java
    Replies: 2
    Last Post: 05-05-2008, 10:44 PM
  5. Taking input from console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 05:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •