473,915 Members | 3,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

52 New Member
Hi,
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.

Thanks
Nov 4 '08 #1
8 33545
JosAH
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); 
  7.  
kind regards,

Jos
Nov 4 '08 #2
shreedhan
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
  3.  
  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..


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

kind regards,

Jos

ps. A simpler getClass().getR esource() als works properly.
Nov 5 '08 #5
shreedhan
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{
  4.  
  5.  
  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);
  12.  
  13.  
  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.     }
  21.  
Nov 5 '08 #6
shreedhan
52 New Member

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

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

Thanks
If this works:

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

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

kind regards,

Jos
Nov 5 '08 #8
shreedhan
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;
  4.  
  5.  
  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));
  12.  
  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.     }
  24.  
  25.  
  26. }
  27.  
  28.  
  29.  
  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;
  40.  
  41.     public sprite_test(){
  42.         .........
  43.     }
  44.  
  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.     }
  58.  
  59. .....................
  60. ....................
  61.  
  62.  
  63.  
  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();
  69.  
  70.         abc.game();               //this method eventually calls loadimage
  71.     }
  72.  
  73. }
  74.  

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.

Thanks
Nov 15 '08 #9

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

Similar topics

1
4464
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 a file images.properties and put some images in a folder called images. By the way this is...
3
11000
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 usenet and in the javadoc tells me I am doing the right thing. I think I found a bug in the JDK,...
1
5360
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
2
1604
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 --
1
2321
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 position myself everything works fine and he is painted the right place. My problem is when my keyPressed...
0
917
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
1
1204
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) {
1
6821
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 file just like below . >>static JLayerPlayer xLayer=new...
5
3855
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 proper path String pathSounds=filePathSounds.substring(6); public xPlayer k= new...
0
10039
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10928
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10543
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9734
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8102
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5944
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4779
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
2
4346
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.