I am trying to code a telephone directory for my java project (MATSEC ‘O’ Levels equivalent to the British GCSE) and while coding (using BlueJ) this error pops up. I am using my teacher’s book as a reference and there is nothing related to the error and it doesn’t say that I should add anything to it. Here is my java code (Not the main class):
import java.io.*;
class Data{
String read(){
String[] name = null;
String[] surname = null;
String[] company = null;
String[] house = null;
String[] street = null;
String[] locality = null;
String[] telno = null;
String[] mobno = null;
int entnum;
BufferedReader txt = new BufferedReader(new FileReader("Directory.txt"));
System.out.println("Name\tSurname\tCompany\tHouse\tStreet\tLocality\tTelephone\tMobile");
System.out.println("\n-----------------------------------------------------------------------------------------------");
for(entnum = 0;name[entnum]!= null; entnum++){
name[entnum] = txt.readLine();
surname[entnum] = txt.readLine();
company[entnum] = txt.readLine();
house[entnum] = txt.readLine();
street[entnum] = txt.readLine();
locality[entnum] = txt.readLine();
telno[entnum] = txt.readLine();
mobno[entnum] = txt.readLine();
System.out.print(name[entnum]+ "\t");
System.out.print(surname[entnum]+ "\t");
System.out.print(company[entnum]+ "\t");
System.out.print(house[entnum]+ "\t");
System.out.print(street[entnum]+ "\t");
System.out.print(locality[entnum]+ "\t");
System.out.print(telno[entnum]+ "\t");
System.out.print(mobno[entnum]+ "\t\n");
}
return null;
}
}
Basically, this simply reads from a text file and displays the entries. I am not yet using GUI.
Your file reading code inside read() method should be wrapped inside try/catch block
(or)
define read() method as
read() throws FileNotFoundException { .....}.FileNotFoundExceptionis checked exception, it should be either declared in throws clause (or) Code which may throw this exception should be wrapped in try/catch due to catch/specify requirement.