Help
Printable View
Help
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
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------Code Java: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(); } } }
--------------------------------------------------------------------------------------------Code Java: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(); } }
Code Java: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.Quote:
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.
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.Quote:
it is still saying there is an error.
Look at line 30 and find the variable with the null value.Quote:
Exception in thread "main" java.lang.NullPointerException
at password.createFile.addRecords(createFile.java:30)