0

I am using this code:

URL excelResources = Excel_file_reader.class.getClass().getResource("/Test_Cases.xlsx"); 

to read an excel file in case I will have to create a jar. I keep getting a null pointer exception.

I ran the exact same project on my personal computer and the code actually runs and the program can read the excel values. But on my work computer the code does not run and results in a null pointer exception. On both computers I created a resources folder and added it to the project path. The excel file is placed in the same locations on both computers. I know the JDK and eclipse versions are different but I don't think that should make a difference. On my work computer I have admin rights too. I have cleaned the project and tried multiple times. When i use

System.out.println(excelResources.getPath());
File excel = new File(excelResources.toURI());
FileInputStream ExcelFile= new FileInputStream(excel);

On my personal computer the path gets printed, but on my work computer it returns as a null pointer.

Please suggest.

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
  • Possible duplicate of [this.getClass().getClassLoader().getResource("...") and NullPointerException](https://stackoverflow.com/questions/3803326/this-getclass-getclassloader-getresource-and-nullpointerexception). Also look [here](https://stackoverflow.com/questions/760756/how-do-you-make-getresourceasstream-work-while-debugging-java-in-eclipse/760789) – paulsm4 Jun 10 '18 at 18:08

1 Answers1

-1

One of these changes I made fixed it.

  • Uninstalled JDKS/JREs and Eclipse
  • Installed JDK 8 (was using 10 on work computer )and Eclipse
  • Created new workspace on desktop instead of in my documents
  • Started project from scratch, copied my old code/packages
  • Did multiple maven cleans and project cleans.

Now the the project runs well on my work computer too. If I had to pick one of the top reasons i would say the third reason but could be the other ones or combination of them :)