NullPointerExeption when getting resources
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   lafaiet_m
Posted On:   Thursday, September 2, 2004 02:52 AM

Hi, I am trying to build a simple program to get a file resource. The code is fairly simple, with the next method in mypackage.MyClass: File someMethod(String filename) { String name = this.getClass().getResource(filename).getFile(); File file = new File(name); return file; } The project is organised as follows: /src - the sources directory /bin - the binaries directory /resources - the directory where my file exists, let's say testfile.html I try to call someMethod("resources/testfile.html") or someMethod("../resources/testfile.   More>>

Hi,


I am trying to build a simple program to get a file resource. The code is fairly simple, with the next method in mypackage.MyClass:


			
File someMethod(String filename) {


String name =
this.getClass().getResource(filename).getFile();


File file = new File(name);


return file;


}




The project is organised as follows:


/src - the sources directory


/bin - the binaries directory


/resources - the directory where my file exists, let's say testfile.html



I try to call someMethod("resources/testfile.html") or
someMethod("../resources/testfile.html").

Also tried to set the classpath in the .ant file:


			















and then call someMethod("testfile.html").


Every time I get NullPointerException, IDE or command line.



Thanks for help!
l.    <<Less

Re: NullPointerExeption when getting resources

Posted By:   Pavan_Gattu  
Posted On:   Friday, September 3, 2004 12:26 AM

Basically you need to add the /resources path to your system or ANT script classpath and then provide the resource file name relative to that classpath. This solves your problem.
About | Sitemap | Contact