Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Confusing error message

2 views
Skip to first unread message

Andy K

unread,
Nov 18, 2003, 7:09:19 AM11/18/03
to
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[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

Sudsy

unread,
Nov 18, 2003, 10:07:54 AM11/18/03
to
Andy K wrote:
> 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[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?

Andy K

unread,
Nov 18, 2003, 10:26:23 AM11/18/03
to
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

unread,
Nov 18, 2003, 11:12:21 AM11/18/03
to
Andy K wrote:
> 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?

0 new messages