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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shreedhan
    New Member
    • Jul 2007
    • 52

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

    Hi,
    I'm very new to java... and I'm having a problem in getting a URL

    Code:
    URL url;
    url=getClass().getClassLoader().getResource(name);
    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
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    No, there is no difference; try it:

    Code:
    URL url;
    System.out.println("name: "+name); 
    url=getClass().getClassLoader().getResource(name);   
    System.out.println(url); 
    url=getClass().getClassLoader().getResource("abc.jpg");   
    System.out.println(url);
    kind regards,

    Jos

    Comment

    • shreedhan
      New Member
      • Jul 2007
      • 52

      #3
      Thanks for your reply

      I tried the above mentioned code and the output was

      Code:
      name: 
      abc.jpg
      
      null
      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

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Show us the code where you have used this name variable.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          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.

          Comment

          • shreedhan
            New Member
            • Jul 2007
            • 52

            #6
            The code I'm using is following..


            Code:
            public BufferedImage loadimage(String name){
            		URL url=null;
            		try{
            
            
            			   System.out.println("name: "+name); 
            			   url=getClass().getClassLoader().getResource(name);     
            			   System.out.println(url); 
            			   url=getClass().getClassLoader().getResource("shreedhan.gif");   
            			   System.out.println(url); 
            return ImageIO.read(url);
            
            			
            		}
            		catch (Exception e){
            			System.out.println("No such image file named "+name+" at "+url);
            			System.exit(0);
            			return null;
            		}
            	}

            Comment

            • shreedhan
              New Member
              • Jul 2007
              • 52

              #7
              Originally posted by JosAH

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

              Thanks

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by shreedhan
                I tried this one as well. But its giving the same result..

                Thanks
                If this works:

                Code:
                URL url= getClass().getResource("abc.jpg");
                ... then this should work too:

                Code:
                String name= "abc.jpg";
                URL url= getClass().getResource(name);
                There is no more I can say about it.

                kind regards,

                Jos

                Comment

                • shreedhan
                  New Member
                  • Jul 2007
                  • 52

                  #9
                  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


                  Code:
                  class parse_Xml {
                  	private Document doc;
                  	public String player_image;
                  	
                  	
                  	public parse_Xml(String file){
                  		try{
                  		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
                  		dbf.setValidating(false);
                  		DocumentBuilder db=dbf.newDocumentBuilder();
                  		doc=db.parse(new File(file));
                  		
                  		}catch(Exception e){
                  			e.printStackTrace();
                  		}
                  	}
                  	public String getImageName(){
                  		NodeList child=doc.getDocumentElement().getChildNodes();
                  		Node root=child.item(0);
                  		Text name=(Text)root;
                  		player_image=name.getNodeValue();
                  		return player_image;                //it returns shreedhan.gif from test.xml
                  	}
                  	
                  	
                  }
                  
                  
                  
                  public class sprite_test extends Canvas {
                  	public static final int HEIGHT=600;
                  	public static final int WIDTH=800;
                  	public String imagename;
                  	public int xpos=0;
                  	public int ypos=HEIGHT/4;
                  	public int dx=10;
                  	public BufferStrategy buff;
                  	public HashMap sprites;
                  	public long usedTime;
                  	
                  	public sprite_test(){
                  		.........
                  	}
                  	
                  	public BufferedImage loadimage(){
                  		URL url=null;
                  		//System.out.println("in load image" + name);
                  		try{
                  			url=getClass().getResource(imagename);
                  			return ImageIO.read(url);
                  		}
                  		catch (Exception e){
                  			System.out.print("No such image file named "+imagename+" at "+url);
                  			System.exit(0);
                  			return null;
                  		}
                  	}
                  
                  .....................
                  ....................
                  
                  
                  
                  public static void main(String args[])
                  	{
                  		sprite_test abc=new sprite_test();
                  		parse_Xml imgObj= new parse_Xml("test.xml");
                  		abc.imagename=imgObj.getImageName();
                  		
                  		abc.game();               //this method eventually calls loadimage
                  	}
                  
                  }

                  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

                  Code:
                  public class parse_Xml{
                  	public String getImageName(){
                  		return "shreedhan.gif";
                  	}
                  }

                  it runs fine.. this way.

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

                  Thanks

                  Comment

                  Working...