Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java Password Saver Problem

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Password Saver Problem

    Help
    Last edited by chesspro13; January 18th, 2013 at 06:31 PM. Reason: did not format


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Java Password Saver Problem

    I am working on a simple password saver that makes a text file and then i can add a website username and password, but whenever i try to add new information it says

    [COLOR="#FF0000"]Exception in thread "main" java.lang.NullPointerException
    at password.createFile.addRecords(createFile.java:32)
    at password.apple.main(apple.java:21)[COLOR]

    Can anyone help me understand why it does not work?

    here is the code
    --------------------------------------------------------------------------------------------
    package password;
    import java.util.*;
     
    public class apple {
    	public static void main(String[] args){
     
    		@SuppressWarnings("resource")
    		Scanner input = new Scanner(System.in);
    		System.out.print("what would you like to do? 'add' or 'see'?");
    		String option = input.nextLine();
     
    		if(option.equals("see")){
    			readFile r = new readFile(); 
    			r.openFile();
    			r.readFile();
    			r.closeFile();
    		}
    		if(option.equals("add")){
    			createFile g = new createFile();
    			//g.openFile();
    			g.addRecords();
    			g.closeFile();
    		}
    	}
    }
    --------------------------------------------------------------------------------------------
    package password;
    import java.io.File;
    import java.io.File.*;
    import java.util.*;
     
    public class createFile {
     
    	public static String web;
    	public static String user;
    	public static String pass;
    	private Formatter y;
    	public Scanner x;
     
    	public void openFile(){
    	try{
    		x = new Scanner(new File("test.txt"));
    		System.out.println("You crested a file");
    	}
    	catch(Exception e){
    		System.out.println("you have an error");
    	}
    	}
    	public void addRecords(){
    		@SuppressWarnings("resource")
    		Scanner input = new Scanner(System.in);
    		System.out.print("Website: ");
    		String web1 = input.nextLine();
    		System.out.print("Username: ");
    		String user = input.nextLine();
    		System.out.print("Password: ");
    		String pass = input.nextLine();
    		y.format("%s %s %s", web1, user, pass);
    	}
     
    		public void closeFile(){
    			x.close();
    		}
     
    }
    --------------------------------------------------------------------------------------------
    package password;
    import java.io.*;
    import java.util.*;
     
    public class readFile {
     
    	public Scanner z;
    	public static int one = 1;
     
    	public void openFile(){
    		try{
    			z = new Scanner(new File("test.txt"));
    		}
    		catch(Exception e){
    			System.out.println("Could not find file");
    		}
    	}
     
    	public void readFile() {
    		if(one == 1){
    			String a = z.next(); 
    			String b = z.next();
    			String c = z.next();
     
    			System.out.printf("%s %s %s", a, b, c);
     
    		}
    	}
    		public void closeFile(){
    			z.close();
    		}	
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Password Saver Problem

    Exception in thread "main" java.lang.NullPointerException
    at password.createFile.addRecords(createFile.java:32)
    Look at line 32 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 32 and print out the values of all the variables on that line.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Password Saver Problem

    I added the println and they all print, but it is still saying there is an error.

    this is what prints:

    what would you like to do? 'add' or 'see'?add
    Website: www.facebook.com
    Username: chesspro13
    Password: password
    www.facebook.com chesspro13 password
    Exception in thread "main" java.lang.NullPointerException
    at password.createFile.addRecords(createFile.java:30)
    at password.apple.main(apple.java:21)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Password Saver Problem

    it is still saying there is an error.
    Printing out values won't fix the problem. It will tell you what is causing the problem so you can fix it.

    Exception in thread "main" java.lang.NullPointerException
    at password.createFile.addRecords(createFile.java:30)
    Look at line 30 and find the variable with the null value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Phase one clean up! (windows style file loader/saver WANTED)
    By JonLane in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: March 8th, 2012, 12:22 AM
  2. Trying to figure out how to make a screen saver.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 59
    Last Post: August 8th, 2011, 07:05 AM
  3. Java password encryption
    By jmorr212 in forum JDBC & Databases
    Replies: 2
    Last Post: January 29th, 2011, 02:33 AM
  4. password.java
    By nickpuma19 in forum Object Oriented Programming
    Replies: 5
    Last Post: November 11th, 2010, 01:29 AM
  5. [SOLVED] java password
    By pwngrammer in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 15th, 2009, 09:49 AM

Tags for this Thread