Skip to content

Commit 412d05d

Browse files
committed
Better throw a NoSuchElementException if no more elements are available because parser has been closed
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1509395 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7e63096 commit 412d05d

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,12 @@ public boolean isClosed() {
381381
}
382382

383383
/**
384-
* Returns an iterator on the records. IOExceptions occurring during the iteration are wrapped in a
384+
* Returns an iterator on the records.
385+
*
386+
* <p>IOExceptions occurring during the iteration are wrapped in a
385387
* RuntimeException.
388+
* If the parser is closed a call to {@code next()} will throw a
389+
* NoSuchElementException.</p>
386390
*/
387391
public Iterator<CSVRecord> iterator() {
388392
return new Iterator<CSVRecord>() {
@@ -410,7 +414,7 @@ public boolean hasNext() {
410414

411415
public CSVRecord next() {
412416
if (CSVParser.this.isClosed()) {
413-
return null;
417+
throw new NoSuchElementException("CSVParser has been closed");
414418
}
415419
CSVRecord next = this.current;
416420
this.current = null;

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,15 @@ public void testCarriageReturnLineFeedEndings() throws IOException {
394394
assertEquals(4, records.size());
395395
}
396396

397-
@Test
397+
@Test(expected = NoSuchElementException.class)
398398
public void testClose() throws Exception {
399399
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
400400
final CSVParser parser = CSVFormat.DEFAULT.withCommentStart('#').withHeader().parse(in);
401401
final Iterator<CSVRecord> records = parser.iterator();
402402
assertTrue(records.hasNext());
403403
parser.close();
404404
assertFalse(records.hasNext());
405-
assertNull(records.next());
406-
assertFalse(records.hasNext());
407-
assertNull(records.next());
405+
records.next();
408406
}
409407

410408
@Test

0 commit comments

Comments
 (0)