import java.io.*;
public class listjava
{
public static void main(String args[]){
Console c = System.console();
char[] pw;
pw = c.readPassword("%s","pw: ");
for (char ch: pw)
c.format("%c ",ch);
c.format("\n");
MyUtility mu = new MyUtility();
while(true)
{
String name = c.readLine("%s","input?: ");
c.format("output : %s \n",mu.doStuff(name));
}
}
}
class MyUtility{
String doStuff (String arg1){
return " result is " + arg1;
}
}
I got an error like this:
Exception in thread "main" java.lang.NullPointerException
at listjava.main(listjava.java:7)
Why is my program wrong?
System.console()is returning null.Quoting Java’s documentation:
So, there are probably no console associated to your JVM. You are probably running your program within Eclipse or other IDE. Try running your program from your system’s command line. It should work.
To run your program from the command line.
listjava.classresidesRun java’s interpreter
$ java listjava