java.util.NoSuchElementException Netbeans

455 Views Asked by At

I have a problem with java.util.NoSuchElementException error in NetBeans, but in Eclipse everything works fine. I dont know what to think about this. In code I show where is buging. I have does .csv files in project folder. Sorry for using Polish language in this project.

    File dostawcy = new File("Dostawcy.csv");
    File magazynp = new File("Magazynp.csv");
    File magazynw = new File("Magazynw.csv");
    File slownik = new File("Slownik.csv");

    Scanner loadDostawcy = new Scanner(dostawcy); // HERE SHOWS BUG
    String syf0 = loadDostawcy.nextLine();


Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at Main.Wczytanie(Main.java:95)
at Main.main(Main.java:19)

I have this in static method called Wczytywanie, so thats why shows "at Main.Wczytanie(Main.java:95)". Im sure there is an element in there because in eclipse it works.

2

There are 2 best solutions below

0
On

From what it seems like, your project isn't loading the csv files. Could just be that you need to specify a path to the files.

Something like:

//Replace with actual path
File dostawcy = new File("resources/csv/Dostawcy.csv");
1
On

I would postulate the following:

The file Dostawcy.csv was correctly being read at a relative location in Eclipse. But now in Netbeans the file is not there. Hence, the following line creates a new empty file:

File dostawcy = new File("Dostawcy.csv");

Then in the following code you actually get an error when trying to call nextLine():

Scanner loadDostawcy = new Scanner(dostawcy);
String syf0 = loadDostawcy.nextLine();        // BUG IS ACTUALLY HERE

If you read the Javadoc for Scanner#nextLine(), you will see that a NoSuchElementException occurs when the next line cannot be read, which would be the case with an empty file.