public class PremiershipLeague
{
static BufferedReader fileInput; // this is needed to input text file
characters
static BufferedReader keyboard = new BufferedReader(new
InputStreamReader(System.in));
// this is needed to input from the keyboard
public static void main(String Args[]) throws IOException
{
String textLine,fileName;
boolean moreToRead = true, fileNotFound;
//open the file
do
{
fileNotFound = false;
System.out.print("File to be read ? "); System.out.flush();
fileName = keyboard.readLine();
try
{
fileInput = new BufferedReader( new FileReader(fileName));
}
catch(FileNotFoundException e)// if file is not found
{
System.out.println("File not found " + e.toString());
fileNotFound = true;
}
} while(fileNotFound);
// input from the file
textLine = fileInput.readLine(); // Unicode format strings
moreToRead = (textLine != null);
while(moreToRead)
{
String teamName;
int points;
Premiership a[] = new Premiership[20]; // create array of object type
Premiership with 20 elements
int i = 0;
StringTokenizer tokens;
// this is needed to be able to split a string object into separate
components
tokens = new StringTokenizer(textLine); // to split the read string
into tokens
teamName = tokens.nextToken();
// the team name component extracted from the string
points = Integer.parseInt(tokens.nextToken());
// the points component extracted from the string
a[i] = new Premiership(teamName, points); /* insert object into the
array using Premiership constructor
from class Premiership*/
i++;
System.out.println (a[i].getTeamName() + " " + a[i].getPoints());
textLine = fileInput.readLine(); // Unicode format string
moreToRead = (textLine != null);
} // end of while (moreToRead)
fileInput.close(); // close file
System.out.println( "File read and closed" );
} // end of main
} // end of class PremiershipLeague
You should actually be getting a NullPointerException. Take a
look here:
> a[i] = new Premiership(teamName, points); /* insert object into the
> array using Premiership constructor
> from class Premiership*/
> i++;
>
> System.out.println (a[i].getTeamName() + " " + a[i].getPoints());
You've incremented the index variable (i) before trying to retrieve the
values you've just set.
As to the other exception, I couldn't reproduce on Linux/JRE 1.4.2.
Perhaps if you included the contents of the text file?
Liverpool 11
Bolton 6
Man-Utd 13
Blackburn 8
Everton 5
Aston-Villa 7
Arsenal 14
Chelsea 13
Fulham 8
Birmingham 11
Charlton 5
Tottenham 4
Man-City 11
Middlesbro 4
Leeds 5
Newcastle 3
Southampton 12
Wolves 1
Portsmouth 9
Leicester 5
I did a cut-and-paste and ran it without problems. Closer examination
of the Exception suggests a possible platform problem. File handles
are a relatively low-level construct.
What JRE and O/S are you using?