Console c = System.console();
Is c null?
Doc:
Answer from McDowell on Stack Overflowpublic static Console console()
Returns the unique Console object associated with the current Java virtual machine, if any.
Returns: The system console, if any, otherwise null.
Console c = System.console();
Is c null?
Doc:
public static Console console()
Returns the unique Console object associated with the current Java virtual machine, if any.
Returns: The system console, if any, otherwise null.
NullPointerException is a RuntimeException, which means it doesn't have to be declared in the method signature.
Replace your code with:
System.out.println("Enter an input string:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println("The input string is:");
System.out.println(str);
If you are trying in a IDE(ex: NetBeans) it might show NullPointerException
It shows proper output in a cmd: code:
public class InputConsole {
public static void main(String[] args) {
System.out.print("Enter something:");
String input = System.console().readLine();
System.out.println("input: "+input);
}
}
Output:

Alternatively you can use Scanner or BufferedReader
From the Javadoc:
Returns the unique Console object associated with the current Java virtual machine, if any.
If there is no console associated to the JVM, the pointed line is the call of a method on a null object, hence the exception.
How do you launch your application?
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 you want to read the username from the standard input, you could use this code:
try {
System.out.print("Enter Username: ");
InputStreamReader streamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(streamReader);
String username = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
That is because System.console()is returning null. The official documentation states (bold is mine to emphasize):
public static Console console()
Returns the unique Console object associated with the current Java virtual machine, if any.
Returns: The system console, if any, otherwise null.
You can see it here.
It has to be System.console(). You are not doing any other operation on an object that would cause a null pointer exception. And as @McDowell has pointed out, System.console() can return a null value
Console console = System.console();
String userinput;
userinput= console.readLine("Enter input: ");
instead of this use the following code; i think System.console is used in .Net platform
String userinput;
InputStreamReader sr =new InputStreamReader(System.in);
BufferReader br=new BufferReader(sr);
userinput=br.readLine();
while(ir.readLine()!=null){
//...
}
This already reads a line, so when you want to actually obtain the line you checked about you get the next one, instead. In the last case, that's null since there are no other lines left.
The check should actually be done with the hasNextLine method, defined for that check:
while(ir.hasNextLine()){
//...
}
You're reading the line twice. The second time, you could potentially hit the end of the file and receive a null value. Introduce your tmp variable outside of the while loop so that you only read it once each time:
String tmp = null;
while((tmp = ir.readLine()) !=null){
sb.append(tmp);
Log.i("recHtml", tmp);
}
Are you running your program from an ide as console.readLine returns null when used from an IDE.
For more details refer to this
If you run it from command line you will not get this error.
System.console() returns null if there is no console.
You can work round this either by adding a layer of indirection to your code or by running the code in an external console and attaching a remote debugger.
Check that the result of calling readLine() is not null (empty). If you check, and cause it not to do anything if its empty, this will solve your issue!
Try checking if readLine() is null.
while ((stringLine = br.readLine()) != null) { }
This will make sure that there is something in that line to read, else it has reached end of file.
In your code you have 3 calls to readLine() each iteration. This could cause issues if for some reason the formatting in your text file is changed(missing a blank line for example). You may be better off making each vehicle on one line, seperated by commas. For example:
Vehicle Details
Fordtest =manufacturer
Focus =model
ioCheck =VIN
09/01/1989 =DateOfMan
d =TaxBand
19900 =Cost
Vehicle Details
Fordtest2 =manufacturer
Focus2 =model
ioCheckVIN2 =VIN
09/01/1989 =DateOfMan
a =TaxBand
1922 =Cost
Then becomes:
Fordtest, Focus, ioCheck, 09/01/1989, d, 19900
Fordtest2, Focus2, ioCheckVIN2, 09/01/1989, a, 1922
This would simplify the code somewhat, as well as reducing chance for error.
Also make sure to close the FileInputStream when you are finished with it. This insures that any resources associated with it are released properly.
try {
FileInputStream fStream = new FileInputStream("AddVehicleFromFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fStream));
...
} finally {
try {
if (fStream != null) {
fStream.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
