Confusing error message

A

Andy K

I get this error message when I run the code below. Your help would be
greatly appreciated.
Error
============================================================
File to be read ? java.io.IOException: The handle is invalid
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:185)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:227)
at java.io.BufferedInputStream.read(Compiled Code)
at java.io.FilterInputStream.read(FilterInputStream.java:97)
at java.io.InputStreamReader.fill(Compiled Code)
at java.io.InputStreamReader.read(InputStreamReader.java:239)
at java.io.BufferedReader.fill(Compiled Code)
at java.io.BufferedReader.readLine(Compiled Code)
at java.io.BufferedReader.readLine(BufferedReader.java:329)
at PremiershipLeague.main(Compiled Code)
Exception in thread "main"
=================================================
here is the code.
import java.io.*; // this is needed to input and output data
import java.util.*; // this is needed to make use of the StringTokenizer

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 = new Premiership(teamName, points); /* insert object into the
array using Premiership constructor
from class Premiership*/
i++;

System.out.println (a.getTeamName() + " " + a.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
 
S

Sudsy

Andy said:
I get this error message when I run the code below. Your help would be
greatly appreciated.
<snip>

You should actually be getting a NullPointerException. Take a
look here:
a = new Premiership(teamName, points); /* insert object into the
array using Premiership constructor
from class Premiership*/
i++;

System.out.println (a.getTeamName() + " " + a.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?
 
A

Andy K

thanks for your help
An example of the text file to use is like this. I am still getting the
same error messege

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
Sudsy said:
Andy said:
I get this error message when I run the code below. Your help would be
greatly appreciated.

<snip>

You should actually be getting a NullPointerException. Take a
look here:
a = new Premiership(teamName, points); /* insert object into
the array using Premiership constructor
from class Premiership*/
i++;

System.out.println (a.getTeamName() + " " + a.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?
 
S

Sudsy

Andy said:
thanks for your help
An example of the text file to use is like this. I am still getting the
same error messege

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top