0

I have this constructor, that mentioned to read an xls file in order to be manipulated by JXCEL API.

Fill(String file, int sheet, int cols, int rows) {
    vService = new VipServiceImpl();        
    java.net.URL url = this.getClass().getResource("vips.xls");

System.out.println(url);  //This gave me  null
    File f;
    try {
        f = new File(url.toURI());
    } catch (URISyntaxException e) {
        f = new File(url.getPath());
    }
    excelHandler = new ExcelHandler(f, sheet, cols, rows);
}

I have to mention that i have put the file vips.xls in the same directory as Fill class file, but it gives me a null pointer exception on the line of new File(url.toURI());

UPDATE: I really do now the difference between the classpath and filesystem path:(

Houssam Badri
  • 2,441
  • 3
  • 29
  • 60
  • 1
    Double-check the file name. – Sotirios Delimanolis Jan 01 '14 at 18:28
  • cpoy the file temporary into another location and try to load it from the other location (e.g. C://)- does the error still exist? – LPH Jan 01 '14 at 18:35
  • `System.out.println(url);` gave me `null` – Houssam Badri Jan 01 '14 at 18:36
  • @user3116916 You're misunderstanding the purpose of `getResource()`. It's meant to get a resource from the classpath. – Sotirios Delimanolis Jan 01 '14 at 18:37
  • I want to create after that a jar file – Houssam Badri Jan 01 '14 at 18:38
  • @SotiriosDelimanolis is there any other configuration of classpath? – Houssam Badri Jan 01 '14 at 18:39
  • `getClass().getResource(path)` loads the resources from the classpath - not as you are expecting from a filesystem path. – LPH Jan 01 '14 at 18:39
  • How are you compiling and building your project? – Sotirios Delimanolis Jan 01 '14 at 18:39
  • it is obvious that i misunderstand from filesystem and classpath, i am compiling with eclipse – Houssam Badri Jan 01 '14 at 18:40
  • @HoussemBdr: could you paste the stacktrace for the exception here? This might give some idea of the context of the exception within your program. – a3.14_Infinity Jan 01 '14 at 18:41
  • i was working fine with new File("vips.xls") and vips.xls was on the root folder of the project – Houssam Badri Jan 01 '14 at 18:42
  • 3
    *"in the same folder"* A 'folder' is a concept of of a graphical user interface that refers to a directory. If you mean directory, please type that. But will this resource be supplied with the app.? By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson Jan 01 '14 at 18:42
  • Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. – Andrew Thompson Jan 01 '14 at 18:43
  • BTW - it would help if you can link to the JavaDocs for the `ExcelHandler` class. It is apparently not [this one](http://bmtechnology.com/javadoc/javadoc_wc-201130115/javadoc_wc_src/com/lenovo/plm/pg/process/mtm/datecontrol/ExcelHandler.html) which only accepts `String`/`InputStream`.. – Andrew Thompson Jan 01 '14 at 18:46
  • Does *"in order to be manipulated by JXCEL API."* mean you need read **and write** access to this resource? – Andrew Thompson Jan 01 '14 at 18:59
  • I would suggest that you post your stack trace and provide details on how you are executing your program if you insist that you are doing things correctly and still getting errors. – torbinsky Jan 01 '14 at 19:03
  • @AndrewThompson yes i want to read it (which worked with File(String)) but i need to embed it in order to export the whole project in jar file – Houssam Badri Jan 01 '14 at 19:03
  • @torbinsky, i am sure i am doing the things correctly, but `System.out.println(url);` gave `null` – Houssam Badri Jan 01 '14 at 19:05
  • 1
    @HoussemBdr You should post a detailed explanation of how you are executing your program such as: Is it packaged in a JAR? Are you using Maven? Note from my answer that I got this example to work fine, so you have an environment issue of some sort. – torbinsky Jan 01 '14 at 19:09

3 Answers3

2

Your example, as provided does work for me, in a new Eclipse project, so this leads me to believe you have an environment problem. The most likely scenario that I can think of is that you are executing the application when it is packaged in a jar. The reason this would be a problem is as follows:

A URI from the classpath is not ALWAYS the same as a file path on your system. You should not be using:

new File(getClass().getResource("vips.xls"))

See File loading by getClass().getResource()

Community
  • 1
  • 1
torbinsky
  • 1,450
  • 10
  • 17
-1

Simply, i should make refresh to my eclipse project after copying the file in the directory

Houssam Badri
  • 2,441
  • 3
  • 29
  • 60
  • Exactly. This was an environment issue. If you had provided us with the information that you are running from Eclipse we might have been able to suggest refreshing/cleaning/compiling your project. I would suggest providing a bit more information like this in your future questions :) – torbinsky Jan 01 '14 at 19:24
  • @torbinsky you are totally right, i will try to be more efficient next time:) – Houssam Badri Jan 01 '14 at 19:31
-2

Add a leading slash to denote the root of the classpath

this.getClass().getResource("/vips.xls");
MillaresRoo
  • 3,808
  • 1
  • 31
  • 37