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.

[4247 byte] By [Soothsayera] at [2007-11-27 6:14:16]
# 1
sorry, line 44 is:"if(read.equals(this.Username))//username already taken"does that mean this.Username = null? or maybe read?*confusion*:(
Soothsayera at 2007-7-12 17:23:38 > top of Java-index,Java Essentials,New To Java...
# 2

ok, after hacking at it for a while I managed to fix it, turns out read DOES equal null when we get to the last line, problem was that the code still tries to compare is lexicographically to the username, which is what causes the exception.

Testing it in the form:

if(read.equals(null))

causes exactly the same problem, because you cant compare nothing lexicographically to something.

The correct test is:

if(read == null)

(counterintuitive when read is a String)

just thought people might be interested.

Fixed code:

import java.lang.*;

import java.io.*;

public class 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;

}

public int saveNew() //return(0)=ok, (1)=username already taken, (2)=other exception

{

try

{

String read = "";

BufferedReader in = new BufferedReader(new FileReader("data/users.txt"));

while(true)

{

read = in.readLine();

System.out.println(read);

if(read == null) break;

if(read.equals(this.Username))//username already taken

{

in.close();

return 1;

}

}

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;

}

}

Soothsayera at 2007-7-12 17:23:38 > top of Java-index,Java Essentials,New To Java...
# 3

Your while loop is reading past the end of the file.

Change this:

while(true)

{

read = in.readLine();

if(read.equals(this.Username))//username already taken

{

in.close();

return 1;

}

}

To this:

while( (read = in.readLine()) != null ) {

if( read.equals(this.Username)) {

in.close();

return 1;

}

}

in.close();

Navy_Codera at 2007-7-12 17:23:38 > top of Java-index,Java Essentials,New To Java...