aspose file tools*
The moose likes Beginning Java and the fly likes Why does cons remain equal to null? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does cons remain equal to null?" Watch "Why does cons remain equal to null?" New topic
Author

Why does cons remain equal to null?

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Any idea what's wrong here? :




[HENRY: EDITED cleaned up code of inflammatory terms]


Jon
David Hulse
Greenhorn

Joined: Apr 09, 2011
Posts: 24

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

Joined: Apr 09, 2011
Posts: 24

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

Joined: Apr 09, 2011
Posts: 24

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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Apr 09, 2011
Posts: 24

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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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

Joined: Dec 20, 2006
Posts: 2516
    
  10

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.


OCUP UML fundamental and ITIL foundation
youtube channel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does cons remain equal to null?