0

I have this 2 lines in my code:

String csvPath = "D:\\myFolder";

Scanner scanner = new Scanner(ReadCSV.class.getResourceAsStream(csvPath + "\\myFile.csv"));

It gets me a null pointer error. I think it is because it is trying to find it under project path. How can I make this work without omitting the scanner? Is it possible to make it work wit getResourceAsStream() ?

brainmassage
  • 1,234
  • 7
  • 23
  • 42

3 Answers3

0

Use the Scanner constructor taking a File:

String csvPath = "D:\\myFolder";
Scanner scanner = new Scanner(new File(csvPath, "myFile.csv"));
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

Scanner class constructor always take input Stream object as an argument .

You can also try this way.

   File file=new File("D:/myFolder/myFile.csv");
   Scanner scanner = new Scanner(file);
   System.out.println(scanner.nextLine());
0

The getResourceAsStream is used for resources within the class path and they always take '/' as path separator.

In your case, use a File as parameter as follows:

Scanner scanner = new Scanner(new File("D:\\myFolder\\myfile.csv"));

Should do.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28