File IO Issues
The class below represents a User entry in a system im trying to implement. It simply holds the username and a hashed version of the password. Im trying to write a method for this class that will save the username and password to two files (add a new user to the database).
The code compiles fine but when i run it I get nullPointerException errors and I really cant figure out why.
import java.lang.*;
import java.io.*;
publicclass Entry
{
String Username;
String Password;
public Entry(String a, String b)
{
Username = a;
Password = armor(b);//hash password and store
}
public String armor(String b)
{
String section = b.substring(3,5);
int crypt = b.hashCode();
crypt = crypt ^ (section.hashCode() * (section.hashCode() + 47));
crypt = crypt % 100000000;
if(crypt < 0) crypt = crypt*-1;
b = String.valueOf(crypt);
if(crypt < 10000000) b ="0" + b;
if(crypt < 1000000) b ="0" + b;
if(crypt < 100000) b ="0" + b;
if(crypt < 10000) b ="0" + b;
if(crypt < 1000) b ="0" + b;
if(crypt < 100) b ="0" + b;
if(crypt < 10) b ="0" + b;
return b;
}
publicint saveNew()//return(0)=ok, (1)=username already taken, (2)=other exception
{
String read;
try
{
BufferedReader in =new BufferedReader(new FileReader("data/users.txt"));
try
{
while(true)
{
read = in.readLine();
if(read.equals(this.Username))//username already taken
{
in.close();
return 1;
}
}
}
catch(EOFException e)//we got to the EOF without matching the username
{
BufferedWriter out =new BufferedWriter(new FileWriter("data/users.txt",true));
out.write(this.Username);
out.newLine();
out.close();
}
}
catch(IOException e)
{
return 2;
}
return 0;
}
}
I get the errors:
Exception in thread "main" java.lang.NullPointerException
at Entry.saveNew(Entry.java:44)
line 44 is the line:
"read = in.readLine();"
(PS. the code doesnt save the password yet, havent got round to it)
thanks.

