Forums Register Login
NPE from K&B example SCJP6
Hi, this is an example from K&B SCJP6 page 458.
I'm getting a null pointer exception, but I'm not sure what needs to be changed.
I tried initallising the char array with char[] pw = new char[1000];
but it made no difference. Could someone explain please?

Exception in thread "main" java.lang.NullPointerException
at io_streams.ConsoleReadIn.main(ConsoleReadIn.java:11)

Thank you!
you must be running this program from eclipse or another IDE.
Yes - I am using Eclipse

Are you able to explain why?

IDEs like Netbeans and Eclipse don't support the console class right now that's why they return null when you call System.console(). This might be fixed in future versions...
The Problem is:

This method returns null according to javadoc when the Workbench or Operating System does not give you a console.

I got an exception too using Eclipse and Windows. It worked fine using a MS DOS prompt.

You can at least add to your program to something like this:

I hope someone can find out how we get a valid console within Eclipse.
As always we strongly recommend that you use the command line to study for the SCJP. All of the questions in the real exam were written in a text file and compiled and run from the command line.

No IDE's were used!

Dear Ankit, Peter & Bert, THANK YOU for taking the time to reply.

Much appreciated.
Something about .... going for a swim. With this tiny ad ...
Rocket Oven Kickstarter - from the trailboss

This thread has been viewed 1027 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 18, 2018 06:56:48.