Results 1 to 9 of 9
- 05-11-2009, 01:52 AM #1
[SOLVED] [newbie] NullPointerException
I got this example out of a book, any idea why it is causing a null pointer exception?
Is it possible to dispose of the passwd variable rather than filling it with blanks?
:confused:
Java Code:import java.io.*; public class ConsoleLogon { public static void main(String[] args) { Console cons = System.console(); String userName = cons.readLine("User name: "); //Error: //Exception in thread "main" java.lang.NullPointerException // at ConsoleLogon.main(ConsoleLogon.java:8) char[] passwd = cons.readPassword("Password: "); if (userName.equals("joe") && passwd.equals("joe")) System.out.println("Logon successful"); else System.out.println("Logon failed"); //fill password field with blanks so as to remove it from memory java.util.Arrays.fill(passwd, ' '); } }
Core Java Volume I Fundamentals 8th Ed. (77)
- 05-11-2009, 02:42 AM #2
Are you actually running it with a console?
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-11-2009, 05:16 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
And is this the complete error message you get?
- 05-11-2009, 09:14 AM #4
I was not compiliing it within the command line (console), thanks Orange.
Is the line
if (userName.equals("joe") && passwd.equals("joe") correct, because somehow the logic is evaluating to the "logon failed" part?
- 05-11-2009, 10:02 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 13
end of if statement missig ")"
- 05-11-2009, 10:50 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Java Code:(userName.equals("joe") && passwd.equals("joe")[B])[/B]
- 05-11-2009, 11:41 AM #7
It's there. Tried to use builder as well, but I get confusing results:
Java Code:import java.io.*; public class ConsoleLogon { //NOTE: Run this from the command line not in eclipse. public static void main(String[] args) { Console cons = System.console(); String userName = cons.readLine("User name: "); //NOTE: bug! StringBuilder builder = new StringBuilder(); char[] passwd = cons.readPassword("Password: "); builder.append(passwd); //debug System.out.println("DEBUG: passwd=" + passwd.toString()); System.out.println("DEBUG: builder=" + builder.toString()); //if (builder=="joe") System.out.println("DEBUG: bingo"); Crashes the program if (userName.equals("joe") && builder.equals("joe")) System.out.println("Logon successful"); else System.out.println("Logon failed"); //fill password field with blanks so as to remove it from memory java.util.Arrays.fill(passwd, ' '); } }
Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.
C:\Users\Administrator\Desktop\Java_playground>dir
Volume in drive C has no label.
Volume Serial Number is 982D-F9F4
C:\Users\Administrator\Desktop\Java_playground>jav ac ConsoleLogon.java
C:\Users\Administrator\Desktop\Java_playground>jav a ConsoleLogon
User name: joe
Password:
DEBUG: passwd=[C@190d11
DEBUG: builder=joe
Logon failed
C:\Users\Administrator\Desktop\Java_playground>
- 05-11-2009, 12:01 PM #8
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 13
DEBUG: passwd=[C@190d11Java Code:System.out.println("DEBUG: passwd=" + passwd.toString());
To convert the char array into a String, useJava Code:new String(passwd);
I die a little on the inside...
Every time I get shot.
- 05-11-2009, 12:13 PM #9
Hi ,
Along with what he has suggested above ,Change the condition checking also to make it work.
Use the below if condition and try
if (userName.trim().equalsIgnoreCase("joe") &&
builder.toString().trim().equalsIgnoreCase("joe"))
-Regards
RamyaLast edited by RamyaSivakanth; 05-11-2009 at 12:18 PM.
Ramya:cool:
Similar Threads
-
[SOLVED] [newbie] java.lang.NullPointerException
By jon80 in forum New To JavaReplies: 5Last Post: 05-07-2009, 05:19 PM -
NullPointerException
By tommyyyy in forum New To JavaReplies: 9Last Post: 03-26-2009, 11:51 PM -
NullPointerException I NEED HELP
By mayhewj7 in forum New To JavaReplies: 2Last Post: 02-13-2009, 09:03 AM -
I get a NullPointerException and don't know why
By hendrix79 in forum New To JavaReplies: 9Last Post: 12-14-2008, 07:18 AM -
NullPointerException
By ravian in forum New To JavaReplies: 2Last Post: 12-07-2007, 05:20 PM
Bookmarks