473,574 Members | 3,920 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getClass().getC lassLoader().ge tResource() problem....

52 New Member
I'm very new to java... and I'm having a problem in getting a URL

Expand|Select|Wrap|Line Numbers
  1. URL url;
  2. url=getClass().getClassLoader().getResource(name);
  3. System.out.println(url);
This code works well when I supply a filename "abc.jpg" directly instead of name. But if I get the filename in "name" string.. then, the returned url value is null.

is there any difference in sending the argument to getResource() in these two different ways.

Nov 4 '08 #1
8 33515
11,448 Recognized Expert MVP
No, there is no difference; try it:

Expand|Select|Wrap|Line Numbers
  1. URL url;
  2. System.out.println("name: "+name); 
  3. url=getClass().getClassLoader().getResource(name);   
  4. System.out.println(url); 
  5. url=getClass().getClassLoader().getResource("abc.jpg");   
  6. System.out.println(url); 
kind regards,

Nov 4 '08 #2
52 New Member
Thanks for your reply

I tried the above mentioned code and the output was

Expand|Select|Wrap|Line Numbers
  1. name: 
  2. abc.jpg
  4. null
  5. file:/home/Shreedhan/workspace/test_4_sep/bin/abc.jpg
It shows "null" when "name" is there..
and it shows full path when "abc.jpg" is there..

I even tried concatenating the path to the name, but then it didn't work..

Nov 5 '08 #3
13,262 MVP
Show us the code where you have used this name variable.
Nov 5 '08 #4
11,448 Recognized Expert MVP
Are you using "name" between double quotes? Don't do that, simply use: name.

kind regards,


ps. A simpler getClass().getR esource() als works properly.
Nov 5 '08 #5
52 New Member
The code I'm using is following..

Expand|Select|Wrap|Line Numbers
  1. public BufferedImage loadimage(String name){
  2.         URL url=null;
  3.         try{
  6.                System.out.println("name: "+name); 
  7.                url=getClass().getClassLoader().getResource(name);     
  8.                System.out.println(url); 
  9.                url=getClass().getClassLoader().getResource("shreedhan.gif");   
  10.                System.out.println(url); 
  11. return ImageIO.read(url);
  14.         }
  15.         catch (Exception e){
  16.             System.out.println("No such image file named "+name+" at "+url);
  17.             System.exit(0);
  18.             return null;
  19.         }
  20.     }
Nov 5 '08 #6
52 New Member

ps. A simpler getClass().getR esource() als works properly.
I tried this one as well. But its giving the same result..

Nov 5 '08 #7
11,448 Recognized Expert MVP
I tried this one as well. But its giving the same result..

If this works:

Expand|Select|Wrap|Line Numbers
  1. URL url= getClass().getResource("abc.jpg");
... then this should work too:

Expand|Select|Wrap|Line Numbers
  1. String name= "abc.jpg";
  2. URL url= getClass().getResource(name);
There is no more I can say about it.

kind regards,

Nov 5 '08 #8
52 New Member
Hi all,
I'm writing again here because I think I figured out the problem (not the solution). Sorry for not being completely descriptive previously.

I have two classes on same file . One of the methods in a class parses an XML file and returns String.
This string is used in another method of another class to load an image.

Now the code

Expand|Select|Wrap|Line Numbers
  1. class parse_Xml {
  2.     private Document doc;
  3.     public String player_image;
  6.     public parse_Xml(String file){
  7.         try{
  8.         DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
  9.         dbf.setValidating(false);
  10.         DocumentBuilder db=dbf.newDocumentBuilder();
  11.         doc=db.parse(new File(file));
  13.         }catch(Exception e){
  14.             e.printStackTrace();
  15.         }
  16.     }
  17.     public String getImageName(){
  18.         NodeList child=doc.getDocumentElement().getChildNodes();
  19.         Node root=child.item(0);
  20.         Text name=(Text)root;
  21.         player_image=name.getNodeValue();
  22.         return player_image;                //it returns shreedhan.gif from test.xml
  23.     }
  26. }
  30. public class sprite_test extends Canvas {
  31.     public static final int HEIGHT=600;
  32.     public static final int WIDTH=800;
  33.     public String imagename;
  34.     public int xpos=0;
  35.     public int ypos=HEIGHT/4;
  36.     public int dx=10;
  37.     public BufferStrategy buff;
  38.     public HashMap sprites;
  39.     public long usedTime;
  41.     public sprite_test(){
  42.         .........
  43.     }
  45.     public BufferedImage loadimage(){
  46.         URL url=null;
  47.         //System.out.println("in load image" + name);
  48.         try{
  49.             url=getClass().getResource(imagename);
  50.             return ImageIO.read(url);
  51.         }
  52.         catch (Exception e){
  53.             System.out.print("No such image file named "+imagename+" at "+url);
  54.             System.exit(0);
  55.             return null;
  56.         }
  57.     }
  59. .....................
  60. ....................
  64. public static void main(String args[])
  65.     {
  66.         sprite_test abc=new sprite_test();
  67.         parse_Xml imgObj= new parse_Xml("test.xml");
  68.         abc.imagename=imgObj.getImageName();
  70.         abc.game();               //this method eventually calls loadimage
  71.     }
  73. }

gives an error like "No such image file named shreedhan.gif at null"

It is getting "shreedhan. gif" as imagename but it's not giving its url using getClass.getCla ssLoader().getR esource()

But when I replace parse_Xml class with a simple class like

Expand|Select|Wrap|Line Numbers
  1. public class parse_Xml{
  2.     public String getImageName(){
  3.         return "shreedhan.gif";
  4.     }
  5. }

it runs fine.. this way.

So, I'm not getting what's wrong here.

Nov 15 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

by: David | last post by:
Hello I'm writting an apllication and i like to display and offscreen image. However my code doesn't seem to work. It compiles and runs properly but What i want is to associate the button of the menu to switch an image (actually to add rectangels offscreen and bring it to the front). The following codes runs properly. You just need to create...
by: Richard | last post by:
I have written a static method that uses the Reflection API to scrape all of the names of the instance variables in a class and their corresponding values; and then returns a hashMap containing a key/value pair of the name of the variable and its corresponding value. My problem is getting the value of the field, and everything I have seen on...
by: Franco Alberto Cardillo | last post by:
Hello everyone. I have the following problem. I am sure that I am doing something very silly, but I cannot figure out what it is. I have the following structure for my Java application: 1) These are the directories I subdivided my code into: ../images ../sound ../libraries
by: Philipp | last post by:
Hello This seems a simple question, please send me to the right FAQ if I missed it: If there is a class Fruits with two classes Apples and Bananas extending Fruits. Apples has a method called isRed() which returns true if the apple is red (Bananas don't have such a method) Is it somehow possible to make code like this: -- pseudo-code --
by: Neverhood | last post by:
Hi folks, I'm trying to make a small game about being a drug dealer, with a 10x8 grid JPanel. On top of that i have another 10x8 grid JPanel as glassPane for the player icon. I have all the properties of the player in a player object class with getter and setter methods for fx his X and Y position in the grid. When i set up his X and Y...
by: Galen Somerville | last post by:
I'm not using Cultures so I just want to pull strings out of a Resource dll. In References there is a reference to the CDS80Eng.dll Partial code in a module --------------- Public Function GetResource() As Boolean Public clsRes As CDS80Eng.CDS80Eng Public Function GetResource() As Boolean
by: StrikeZero | last post by:
Hello, I am trying to extend my class Point in class Point2D, but I always get an error that it can not find the symbol Point. This is my code: package Package1DShapes;// a folder that has class point and class algebra inside public class Point { public int x; public Point(int x) {
by: desturrr | last post by:
I am having diffuculties while trying to make the path of my files relative. I am using an interface to hold my static variables in order not to mix them with my other codes , just implementing the interface is sufficient to use the variables. I need to give the address of my file as a parameter, and i am making it by using absolute path of...
by: desturrr | last post by:
I have written a java application , and i am using sounds , images which are placed in Sounds and images packages. I am having trouble about this paths. filePathSounds=getClass().getResource("/Sounds").toString(); // i get the substring because there is file:/C:/JavaProjec.... // i am removing "file:/ "which is 6 characters and getting the...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.