Results 1 to 3 of 3
- 03-18-2011, 12:36 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
Problem with String introduction-read
reply: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); } } }
What the problem?Java Code:Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:10)
- 03-18-2011, 02:09 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
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. "
- 03-19-2011, 01:38 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
How to read a string in a ArrayList?
By warchieflll in forum New To JavaReplies: 6Last Post: 12-09-2010, 05:58 PM -
Basic string problem, just need to extract two numbers out of string
By gonzoateafly in forum New To JavaReplies: 6Last Post: 12-06-2010, 09:26 AM -
How to read/output a JSON as a normal String variable
By Jman85 in forum New To JavaReplies: 8Last Post: 11-12-2010, 03:00 PM -
scanner to read string or int
By joecast in forum New To JavaReplies: 3Last Post: 05-04-2010, 12:09 AM -
Read a string from a txt file
By cachi in forum Java AppletsReplies: 1Last Post: 08-07-2007, 07:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks