Results 1 to 3 of 3
  1. #1
    czarny020 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Problem with String introduction-read

    Java Code:
    import java.io.*; // brakuje ci tego wiersza
    import java.util.Scanner;
    
    public class Main
      {
        public static void main(String args[])
        {
            //z wyświetleniem podpowiedzi
            Console con=System.console();
            String txt=con.readLine("Napisz cokolwiek a potem naciśnij ENTER ");
            System.out.println(txt);
            //z wyświetleniem podpowiedzi i rozbiciem na słowa
            System.out.println("Napisz cokolwiek a potem naciśnij Ctrl-C");
            Scanner sc=new Scanner(System.in);
            while(sc.hasNext())
            {
                String xtx=sc.next();
                System.out.println(xtx);
            }
        }
    }
    reply:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
            at Main.main(Main.java:10)
    What the problem?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Read the API for the classes and methods you are using, some of which may return null. For instance:
    System.console() - "returns The system console, if any, otherwise null. "

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by czarny020 View Post
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
            at Main.main(Main.java:10)
    What the problem?
    Check what you are doing on line 10 of Main.java. And read the API about the error.

    One more thing, even it's not an error (actually some of the advanced IDEs not allowed that) don't use Mainas a class name. main is used to named the main method in a Java class and may unclear when you are reading next time. It's a good practice not to do such things when you are coding. :)

Similar Threads

  1. How to read a string in a ArrayList?
    By warchieflll in forum New To Java
    Replies: 6
    Last Post: 12-09-2010, 06:58 PM
  2. Replies: 6
    Last Post: 12-06-2010, 10:26 AM
  3. Replies: 8
    Last Post: 11-12-2010, 04:00 PM
  4. scanner to read string or int
    By joecast in forum New To Java
    Replies: 3
    Last Post: 05-04-2010, 01:09 AM
  5. Read a string from a txt file
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 08:50 AM

Posting Permissions

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