File Reader problem

Hi,

I am using the following code to read from a text file but whenever the file is not empty i get a NullPointer Exception. May someone help plaease?

void readScores(Player [] playertable){

i=0;

try{

FileReader filereader=new FileReader("HighScores.txt");

BufferedReader buffread=new BufferedReader(filereader);

boolean eof=false;

while(!eof){

String line=buffread.readLine();

if(line==null){

eof=true;}

else{

StringTokenizer tokenizer=new StringTokenizer(line);

playertable[i].name=tokenizer.nextToken();

playertable[i].score=Integer.parseInt(tokenizer.nextToken());

i++;}

buffread.close();}}

catch(IOException e){ System.out.println("Exception "+e.toString());}}

[1572 byte] By [asdgfa] at [2007-10-3 3:48:27]
# 1

Your playertable probably doesn't contain any Players. Try this:

else{

playertable[i] = new Player(); // ADD THIS LINE

StringTokenizer tokenizer=new StringTokenizer(line);

And then rearrange your braces so that the "close" is done outside the while loop.

And, the typical way to do this loop is without an "eof" flag:

String line;

while ((line=buffread.readLine()) != null)

{

StringTokenizer tokenizer = new StringTokenizer(line);

...

}

doremifasollatidoa at 2007-7-14 21:45:29 > top of Java-index,Java Essentials,Java Programming...
# 2
I've been stupid, you've been helpfullThanks a lot..
asdgfa at 2007-7-14 21:45:29 > top of Java-index,Java Essentials,Java Programming...
# 3
You're welcome. But, don't worry. You're not stupid. You just haven't learned everything about Java yet.
doremifasollatidoa at 2007-7-14 21:45:29 > top of Java-index,Java Essentials,Java Programming...