Confusing error message

Discussion in 'Java' started by Andy K, Nov 18, 2003.

  1. Andy K

    Andy K Guest

    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
    Andy K, Nov 18, 2003
    #1
    1. Advertising

  2. Andy K

    Sudsy Guest

    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 = 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?
    Sudsy, Nov 18, 2003
    #2
    1. Advertising

  3. Andy K

    Andy K Guest

    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 wrote:
    > 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 = 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?
    >
    Andy K, Nov 18, 2003
    #3
  4. Andy K

    Sudsy Guest

    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?
    Sudsy, Nov 18, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bazza Formez
    Replies:
    4
    Views:
    565
    Bazza Formez
    Jan 27, 2005
  2. David McBride
    Replies:
    2
    Views:
    498
    David McBride
    Oct 2, 2003
  3. Replies:
    1
    Views:
    906
    bjeremy
    Jan 9, 2007
  4. Wilson

    Confusing Error Message

    Wilson, Mar 23, 2007, in forum: C++
    Replies:
    12
    Views:
    672
    Old Wolf
    Mar 28, 2007
  5. Lloyd Sheen

    Confusing error message

    Lloyd Sheen, Aug 16, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    457
    Lloyd Sheen
    Aug 16, 2008
Loading...

Share This Page