• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does cons remain equal to null?

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea what's wrong here? :




[HENRY: EDITED cleaned up code of inflammatory terms]
 
David Hulse
Greenhorn
Posts: 24
Mac MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to run this program from the command prompt or terminal. Netbeans doesn't count as a console apparently.


I don't know what kind of software you've got but this works for me on a Mac with the bash terminal java -jar <jar name>

you also might want to take out the firstName variable initialization because it doesn't have a corresponding prompt for the user and it seems repetitive.

 
David Hulse
Greenhorn
Posts: 24
Mac MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what kind of software you've got but this works for me on a Mac with the bash terminal java -jar <jar name>
 
David Hulse
Greenhorn
Posts: 24
Mac MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you also might want to take out the firstName variable initialization because it doesn't have a corresponding prompt for the user and it seems repetitive.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Hulse wrote:I don't know what kind of software you've got but this works for me on a Mac with the bash terminal java -jar <jar name>


Well I am using Eclipse 3.6.2, however, I tried it in command prompt, and, it seems like the for ... each loop is not working well, since, I only get the last character of the password returned:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Jon>cd My Documents

C:\Documents and Settings\Jon\My Documents>cd Working Folder

C:\Documents and Settings\Jon\My Documents\Working Folder>cd JavaDev

C:\Documents and Settings\Jon\My Documents\Working Folder\JavaDev>cd myCode Snip
pets

C:\Documents and Settings\Jon\My Documents\Working Folder\JavaDev\myCode Snippet
s>prompt $g

>dir
Volume in drive C has no label.
Volume Serial Number is D80F-8634

Directory of C:\Documents and Settings\Jon\My Documents\Working Folder\JavaDev\
myCode Snippets

10/04/2011 20:00 <DIR> .
10/04/2011 20:00 <DIR> ..
10/04/2011 11:19 603 ArrayList_Playground.java
11/05/2008 17:34 7,210 ContainerEventDemo.java
09/04/2011 19:14 <DIR> equalsMethod
08/05/2008 10:27 271 ListFonts.java
21/04/2008 20:04 480 Password.java
10/04/2011 20:00 885 TestThree.java
09/05/2008 08:12 68,085 venn1.java
6 File(s) 77,534 bytes
3 Dir(s) 38,284,107,776 bytes free

>javac TestThree.java

>java TestThree
What's your name?Jon C.
C.
How old are you?31
Hello, Jon C., next year you will be 32 years old.Enter a password:
[Password :]
Password keyed in: s

Excuse the inflammatory terms, btw, it was only intended as a study program I'm using Win XP SP3.

When I try this within Eclipse I get the following:
Enter a password:
Exception in thread "main" java.lang.NullPointerException
at TestThree.main(TestThree.java:21) - line 21 refers to

 
David Hulse
Greenhorn
Posts: 24
Mac MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
initialize the password string to ""

String password = "";

then in the for each loop instead of password = do password += this will add each char to the string.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed the flaws, here's the working version, thanks for feedback peeps


This code works when compiling through the command line (Win XP), however, Eclipse does not seem to enjoy compiling my code, because it outputs:

 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Hulse wrote:You need to run this program from the command prompt or terminal. Netbeans doesn't count as a console apparently.

Jon, this also applies for Eclipse, and probably every other IDE. That's why you get a NullPointerException - System.console() returns null.

You get the NullPointerException before the "Enter a password" prompt because the NullPointerException is sent to System.err and the prompt to System.out, and Eclipse is quite bad at interweaving these two as you expect.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
David Hulse wrote:You need to run this program from the command prompt or terminal. Netbeans doesn't count as a console apparently.

Jon, this also applies for Eclipse, and probably every other IDE. That's why you get a NullPointerException - System.console() returns null.


That's too bad, I thought Eclipse and Netbeans were fairly reliable.
 
Jan Cumps
Bartender
Posts: 2588
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:
That's too bad, I thought Eclipse and Netbeans were fairly reliable.

They are reliable.

From the Console API:

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
...
If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method.
If no console device is available then an invocation of that method will return null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic