File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NPE from K&B example SCJP6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NPE from K&B example SCJP6" Watch "NPE from K&B example SCJP6" New topic

NPE from K&B example SCJP6

Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140
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(

Thank you!

S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
you must be running this program from eclipse or another IDE.

Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140
Yes - I am using Eclipse

Are you able to explain why?

Ankit Garg

Joined: Aug 03, 2008
Posts: 9466

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...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Peter Heide
Ranch Hand

Joined: Nov 04, 2006
Posts: 31
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.

Bert Bates

Joined: Oct 14, 2002
Posts: 8898
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!

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140

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

Much appreciated.
I agree. Here's the link:
subject: NPE from K&B example SCJP6
It's not a secret anymore!