NoSuchElementException

Discussion in 'Java' started by Alan, Mar 5, 2008.

  1. Alan

    Alan Guest

    I call a method of mine, getFileList, which returns an ArrayList of
    files and is stored in MyFileList. I know how to iterate through the
    ArrayList, but how do I open the file for reading?

    The code below did not work. It compiled, but I got an exception
    where I tried to instantiate a BufferedReader when I executed it:

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.AbstractList$Itr.next(AbstractList.java:427)
    at Test.main(Test.java:31)

    I think I don`t know the proper way to use fileIter.hasNext() to
    initialize the reader.

    Anyone know what I am doing wrong? Thanks, Alan

    ArrayList<File> MyFileList = new ArrayList<File> ();
    .. . .
    MyFileList = FileList.getFileList( CurrentDirectory, translit, false,
    false );
    .. . .
    for ( Iterator<File> fileIter = MyFileList.iterator();
    fileIter.hasNext(); )
    {
    BufferedReader infile = new BufferedReader(new InputStreamReader(new
    FileInputStream(fileIter.next().getName()),"UTF-8"));
    }
     
    Alan, Mar 5, 2008
    #1
    1. Advertising

  2. Alan wrote:
    > I call a method of mine, getFileList, which returns an ArrayList of
    > files and is stored in MyFileList. I know how to iterate through the
    > ArrayList, but how do I open the file for reading?
    >
    > The code below did not work. It compiled, but I got an exception
    > where I tried to instantiate a BufferedReader when I executed it:
    >
    > Exception in thread "main" java.util.NoSuchElementException
    > at java.util.AbstractList$Itr.next(AbstractList.java:427)
    > at Test.main(Test.java:31)
    >
    > I think I don`t know the proper way to use fileIter.hasNext() to
    > initialize the reader.
    >
    > Anyone know what I am doing wrong? Thanks, Alan
    >
    > ArrayList<File> MyFileList = new ArrayList<File> ();
    > . . .
    > MyFileList = FileList.getFileList( CurrentDirectory, translit, false,
    > false );
    > . . .
    > for ( Iterator<File> fileIter = MyFileList.iterator();
    > fileIter.hasNext(); )
    > {
    > BufferedReader infile = new BufferedReader(new InputStreamReader(new
    > FileInputStream(fileIter.next().getName()),"UTF-8"));
    > }


    The exception means that there are no more elements when you call
    next(). From the code you posted I don't see a problem. Is there more
    code?

    You can simplify getting a BufferedReader to
    new BufferedReader(new FileReader(File f))

    --

    Knute Johnson
    email s/nospam/knute/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
     
    Knute Johnson, Mar 5, 2008
    #2
    1. Advertising

  3. Alan

    Roedy Green Guest

    On Tue, 4 Mar 2008 19:08:52 -0800 (PST), Alan
    <> wrote, quoted or indirectly quoted someone
    who said :

    >for ( Iterator<File> fileIter = MyFileList.iterator();
    >fileIter.hasNext(); )


    if myFileList is a collection of Files, (note lower case m), you can
    use the new for:each iteration syntax

    for (File f : myFileList )
    {

    }

    What do you do with a File? see
    http://mindprod.com/applet/fileio.html
    for sample code.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Mar 5, 2008
    #3
  4. Alan

    Roedy Green Guest

    Roedy Green, Mar 5, 2008
    #4
  5. Alan

    Alan Guest

    I was using the next() method twice!
     
    Alan, Mar 6, 2008
    #5
  6. Alan wrote:
    > I was using the next() method twice!


    That will do it!

    --

    Knute Johnson
    email s/nospam/knute/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Mar 6, 2008
    #6
    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.

Share This Page