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

