Help
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.
Help
Last edited by chesspro13; January 18th, 2013 at 06:31 PM. Reason: did not format
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(); } }
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.Exception in thread "main" java.lang.NullPointerException
at password.createFile.addRecords(createFile.java:32)
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.
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)
Printing out values won't fix the problem. It will tell you what is causing the problem so you can fix it.it is still saying there is an error.
Look at line 30 and find the variable with the null value.Exception in thread "main" java.lang.NullPointerException
at password.createFile.addRecords(createFile.java:30)
If you don't understand my answer, don't ignore it, ask a question.