Cannot throw EOFException

Hello,

I've been trying to do a little exercise about IO and I'm frustrated due to a NullPointerException which is caused, I am pretty sure, by failing to throw and end of file exception. Could somebody tell me why isn't it working the way I intended?

package sj;

import java.io.*;

import java.util.StringTokenizer;

publicclass Main{

publicstaticvoid main(String[] args)throws IOException{

RandomAccessFile raf =new RandomAccessFile("sjdb.txt","rw");

IOManager manager =new IOManager();

manager.readFile(raf);

raf.close();

System.out.println("finished");

}

}

class Student{

String surname;

String name;

int id;

int [] birthdate =newint [3];

String birthplace;

String domain;

int yearEntered;

Student(String surname, String name,int id,int birthYear,int birthMonth,int birthDay,

String birthplace, String domain,int yearEntered){

this.surname = surname;

this.name = name;

this.id = id;

this.birthdate[0] = birthYear;

this.birthdate[1] = birthMonth;

this.birthdate[2] = birthDay;

this.birthplace = birthplace;

this.domain = domain;

this.yearEntered = yearEntered;

System.out.println("New student object created.");

}

Student [] addtoStack(Student newStudent, Student [] oldStack){

Student [] newStack =new Student [oldStack.length + 1];

for (int i = 0; i < oldStack.length; i++)

newStack[i] = oldStack[i];

newStack[oldStack.length] = newStudent;

System.out.println("Student added to list.");

return newStack;

}

}

class IOManager{

String line ="";

String surname;

String name;

int id;

String birthdate;

int birthYear;

int birthMonth;

int birthDay;

String birthplace;

String domain;

int yearEntered;

StringTokenizer st;

Student [] studentStack =new Student [0];

boolean nameflag =false;

boolean placeflag =false;

void readFile (RandomAccessFile raf)throws IOException{

raf.seek(0);

while (true){

try{

line = raf.readLine();

st =new StringTokenizer(line);

surname = st.nextToken();

System.out.println("Surname: " + surname);

name ="";

do{

if (!nameflag)

name = st.nextToken();

else name = name +" " + st.nextToken();

nameflag =true;

}

while (st.hasMoreTokens());

nameflag =false;

System.out.println("Name: " + name);

line = raf.readLine();

st =new StringTokenizer(line);

id = Integer.parseInt(st.nextToken());

System.out.println("Number: " + id);

birthplace ="";

while (st.hasMoreTokens())

{

String token = st.nextToken();

if (token.charAt(0) >='0' && token.charAt(0) <='9')

{

birthdate = token;

break;

}

else{

if (!placeflag)

{

birthplace = token;

placeflag =true;

}

else birthplace = birthplace +" " + token;

}

}

placeflag =false;

System.out.println("Birth place: " + birthplace);

birthDay = Integer.parseInt(birthdate.substring(0,2));

birthMonth = Integer.parseInt(birthdate.substring(3,5));

birthYear = Integer.parseInt(birthdate.substring(6,10));

System.out.println("Date of birth: " + birthDay +"." + birthMonth +"." + birthYear);

line = raf.readLine();

if (line.charAt(0)=='F')

domain ="F";

elseif (line.charAt(0) =='T')

domain ="TM";

else domain ="S";

line = raf.readLine();

st =new StringTokenizer(line);

yearEntered = Integer.parseInt(st.nextToken());

System.out.println("Alan: " + domain);

System.out.println("Year of immatriculation: " + yearEntered);

Student newStudent =new Student(surname, name, id, birthYear, birthMonth,

birthDay, birthplace, domain, yearEntered);

studentStack = newStudent.addtoStack(newStudent, studentStack);

raf.readLine();

System.out.println("\n-\nList length: " + studentStack.length);

}

catch (EOFException e){

System.out.println("end of file.");

break;

}

}

}

}

and the file sjdb.txt looks like an extended version of this:

DOE John

8133 London 11.01.1986

Turkish Literature

2000 2005

DOE Jane

8203 New York 15.02.1987

Science

2001 2005

DOE John

8133 London 11.01.1986

Turkish Literature

2000 2005

DOE Jane

8203 New York 15.02.1987

Science

2001 2005

DOE John

8133 London 11.01.1986

Turkish Literature

2000 2005

DOE Jane

8203 New York 15.02.1987

Science

2001 2005

[8613 byte] By [ali_sinana] at [2007-10-3 3:39:22]
# 1

If you don't understand why you're getting NPE instead of EOFE, you might want to read the docs for readline again.

[url http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html#readLine()]http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html#readLine()[/url]

jverda at 2007-7-14 21:34:50 > top of Java-index,Core,Core APIs...
# 2

Thanks for your help to reminding me the correct use of the docs :), now I've figured out the reason, and I've managed to get an EOFE by adding this inside the loop:

raf.readByte();

raf.seek(raf.getFilePointer()-1);

But why can't readLine() be correctly used with this exception, is there any other way to throw an EOFE with readLine() ?

ali_sinana at 2007-7-14 21:34:50 > top of Java-index,Core,Core APIs...
# 3
This new code is all nonsense. You need to read the specification of readLine() again. And ask yourself whether the objective is to throw or catch EOFException, or to detect the end of the file?
ejpa at 2007-7-14 21:34:50 > top of Java-index,Core,Core APIs...
# 4

> Thanks for your help to reminding me the correct use

> of the docs :), now I've figured out the reason, and

> I've managed to get an EOFE by adding this inside the

> loop:

>

> > raf.readByte();

> raf.seek(raf.getFilePointer()-1);

>

That's the wrong approach. Read the doc again. Carefully. You're overcomplicating it. The doc and the NPE you're getting should give you a simpler way to know when you've read the whole file.

> But why can't readLine() be correctly used with this

> exception, is there any other way to throw an EOFE

> with readLine() ?

Why do you want to throw EOFE?

jverda at 2007-7-14 21:34:50 > top of Java-index,Core,Core APIs...
# 5

Excuse me if I sound uncareful, but I think I've focused way too much on getting that EOFE (which I see now that isn't throwed by readLine() ). Sorry for my previous supposed solution too, it didn't seem any correct to me either.

So making a simple test to verify if the next line i read is null simply tells me if I reached the end of the file, if I get it right.

Thank you for your help, jverd and ejp.

ali_sinana at 2007-7-14 21:34:50 > top of Java-index,Core,Core APIs...
# 6
> So making a simple test to verify if the next line i> read is null simply tells me if I reached the end of> the file, if I get it right.Yep!
jverda at 2007-7-14 21:34:50 > top of Java-index,Core,Core APIs...