473,289 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

getClass().getClassLoader().getResource() problem....

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 33487
JosAH
11,448 Expert 8TB
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
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 8TB
Show us the code where you have used this name variable.
Nov 5 '08 #4
JosAH
11,448 Expert 8TB
Are you using "name" between double quotes? Don't do that, simply use: name.

kind regards,

Jos

ps. A simpler getClass().getResource() als works properly.
Nov 5 '08 #5
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

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

Thanks
Nov 5 '08 #7
JosAH
11,448 Expert 8TB
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
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.getClassLoader().getResource()



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
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...
3
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...
1
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)...
2
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...
1
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...
0
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...
1
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...
1
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...
5
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. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.