Results 1 to 9 of 9
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [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, ' ');
    
    	}
    
    }
    Reference:
    Core Java Volume I Fundamentals 8th Ed. (77)

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    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?

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Java Code:
    (userName.equals("joe") && passwd.equals("joe")[B])[/B]

  7. #7
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    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, ' ');
    		
    		
    
    	}
    
    }
    Output:
    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>

  8. #8
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    DEBUG: passwd=[C@190d11
    Java Code:
    System.out.println("DEBUG: passwd=" + passwd.toString());
    passwd is a char array - you're printing the reference to that array.
    To convert the char array into a String, use
    Java Code:
    new String(passwd);
    I die a little on the inside...
    Every time I get shot.

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    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
    Ramya
    Last edited by RamyaSivakanth; 05-11-2009 at 11:18 AM.
    Ramya:cool:

Similar Threads

  1. [SOLVED] [newbie] java.lang.NullPointerException
    By jon80 in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:19 PM
  2. NullPointerException
    By tommyyyy in forum New To Java
    Replies: 9
    Last Post: 03-26-2009, 10:51 PM
  3. NullPointerException I NEED HELP
    By mayhewj7 in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 08:03 AM
  4. I get a NullPointerException and don't know why
    By hendrix79 in forum New To Java
    Replies: 9
    Last Post: 12-14-2008, 06:18 AM
  5. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •