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
-
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.getClassLoader().getResource()
But when I replace parse_Xml class with a simple class like
- 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