469,265 Members | 1,962 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,265 developers. It's quick & easy.

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 32537
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

Post your reply

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

Similar topics

1 post views Thread by David | last post: by
1 post views Thread by Franco Alberto Cardillo | last post: by
1 post views Thread by Neverhood | last post: by
reply views Thread by Galen Somerville | last post: by
1 post views Thread by StrikeZero | last post: by
5 posts views Thread by desturrr | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.