Results 1 to 7 of 7
  1. #1
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default Using Console Class

    I am having a little trouble using the Console class. I imported the class as well as read on the methods that I need to complete the job. However, it keeps giving a Null Pointer exception.

    Java Code:
    Console console = System.console();
            String username = console.readLine("User: ");
    The error is this: Exception in thread "main" java.lang.NullPointerException
    at Next_Palindrome.NextPalindrome.main(NextPalindrome .java:11)
    Java Result: 1

    Any ideas? This is all the code that is running btw.

    Thanks!
    Tabish Chasmawala

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default

    What variable is null? Can the console() method run a null value? What does the API doc say?
    How and where are you executing the program?

  3. #3
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default

    It catches the exception at:
    Java Code:
    String username = console.readLine("User: ");
    The console() method passes a null exception if the end of stream has been reached. But I have not typed anything as yet. I use the class BufferedReader perfectly, and wanted to try this class.
    I am executing the program using the Netbeans IDE on a Windows PC.
    Tabish Chasmawala

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default

    Print out the value of the console variable to see if it is null.
    Did you read the API doc for the System class's console() method?


    The console() method passes a null exception if the end of stream has been reached
    Where did you see it worded like this?
    Last edited by Norm; 08-13-2011 at 03:45 PM.

  5. #5
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default

    I tried printing out the value of the console variable using this code:
    Java Code:
    Console console = System.console();
            System.out.println(console);
            System.out.println(console.toString());
    And nothing was printed. Oh and sorry, I thought you were talking about the readLine method. But The console() method passes null if no console is associated with the current Java virtual machine.
    Tabish Chasmawala

  6. #6
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default

    Never mind... I did a little more searching and found my answer.

    Stupid Java Tricks Using the New java.io.Console Class

    Thanks for the help tho.
    Tabish Chasmawala

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default

    And nothing was printed.
    That's strange. I would expect it to print one of these:
    Java Code:
          Console console = System.console();
          System.out.println("console=" + console);   
          // console=null
          //console=java.io.Console@42e816

Similar Threads

  1. Help with Console
    By thiagohe in forum Advanced Java
    Replies: 7
    Last Post: 08-01-2011, 01:30 PM
  2. [SOLVED] ReadLine(String fmt,Object... args) of Console class
    By Pooja Deshpande in forum New To Java
    Replies: 4
    Last Post: 04-25-2008, 05:51 AM
  3. Java Swing class capturing output to the console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:24 AM
  4. Reading a line from console using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 11:52 AM
  5. Read from console (Scanner Class)
    By hey in forum New To Java
    Replies: 10
    Last Post: 12-11-2007, 10:11 PM

Posting Permissions

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