matthew016
asked on
Error when getting ressource in JApplet
Hi,
In my HTML page I use a JApplet,
in the code tag I have my JApplet class,
in the archive tag I have the name of the .jar
<applet code="client.ui.Client_GUI _Frame.cla ss"
archive="Client.jar"
name="Le Hall"
...
I have this exception :
java.lang.NullPointerExcep tion
at javax.swing.ImageIcon.<ini t>(Unknown Source)
at client.ui.AnimationPane.cr éerActions SecToolbar Contacts(A nimationPa ne.java:95 )
...
I have the error at this line :
Toolkit.getDefaultToolkit( ).getImage (ClassLoad er.getSyst emResource (path));
The path is correct
In my HTML page I use a JApplet,
in the code tag I have my JApplet class,
in the archive tag I have the name of the .jar
<applet code="client.ui.Client_GUI
archive="Client.jar"
name="Le Hall"
...
I have this exception :
java.lang.NullPointerExcep
at javax.swing.ImageIcon.<ini
at client.ui.AnimationPane.cr
...
I have the error at this line :
Toolkit.getDefaultToolkit(
The path is correct
ASKER
Thx, but I have an error with getCodeBase,
it is not in the JApplet class, (in a abstract class)
it is not in the JApplet class, (in a abstract class)
ASKER
I use getCodeBase not in the class that extends JApplet class, but in an other class of my JApplet.
I have an error with getCodeBase()
(my code works whn run it from Eclipse before posting this questio, but not from the html)
I have an error with getCodeBase()
(my code works whn run it from Eclipse before posting this questio, but not from the html)
Try something like
Image i = javax.imageio.ImageIO(getC lass().get ResourceAs Stream("/i mages/x.jp g"));
Image i = javax.imageio.ImageIO(getC
make sure your image is in your jar, and use:
ImageIcon ii = new ImageIcon(getClass().getRe source(pat h));
ImageIcon ii = new ImageIcon(getClass().getRe
ASKER
>>CEHJ
>>Image i = javax.imageio.ImageIO(getC lass().get ResourceAs Stream("/i mages/x.jp g"));
this doesn't compile
>>objects
>>make sure your image is in your jar
Yes they are in the jar, I can see it trough winrar
>>ImageIcon ii = new ImageIcon(getClass().getRe source(pat h));
I have an error at getClass(), it says rename in file, why ?
+ I need an Image, not ImageIcon, I suppose I can convert
>>Image i = javax.imageio.ImageIO(getC
this doesn't compile
>>objects
>>make sure your image is in your jar
Yes they are in the jar, I can see it trough winrar
>>ImageIcon ii = new ImageIcon(getClass().getRe
I have an error at getClass(), it says rename in file, why ?
+ I need an Image, not ImageIcon, I suppose I can convert
>>>>
>>CEHJ
>>Image i = javax.imageio.ImageIO(getC lass().get ResourceAs Stream("/i mages/x.jp g"));
this doesn't compile
>>>>
Sorry
Image i = javax.imageio.ImageIO.read (getClass( ).getResou rceAsStrea m("/images /x.jpg"));
>>CEHJ
>>Image i = javax.imageio.ImageIO(getC
this doesn't compile
>>>>
Sorry
Image i = javax.imageio.ImageIO.read
> I have an error at getClass(), it says rename in file, why ?
can u post the exact error
> + I need an Image, not ImageIcon, I suppose I can convert
no need, you can access Image using getImage() method
ASKER
I guess I can't use getClass because i'm in a static method
ASKER
public static Image getImage(String path){
return javax.imageio.ImageIO.read (getClass( ).getResou rceAsStrea m("/images /x.jpg")); //error
}
return javax.imageio.ImageIO.read
}
> I guess I can't use getClass because i'm in a static method
correct, in that case just use the class anme directly:
public static Image getImage(String path){
return new ImageIcon(MyClass.class.ge tResource( path)).get Image();
// or return javax.imageio.ImageIO.read (MyClass.c lass.getRe sourceAsSt ream("/ima ges/x.jpg" ));
}
correct, in that case just use the class anme directly:
public static Image getImage(String path){
return new ImageIcon(MyClass.class.ge
// or return javax.imageio.ImageIO.read
}
> at javax.swing.ImageIcon.<ini t>(Unknown Source)
u appear to be creating an ImageIcon, so why not return it directly
public static ImageIcon getImage(String path){
return new ImageIcon(MyClass.class.ge tResource( path));
}
u appear to be creating an ImageIcon, so why not return it directly
public static ImageIcon getImage(String path){
return new ImageIcon(MyClass.class.ge
}
ASKER
> u appear to be creating an ImageIcon, so why not return it directly
Yes I know, but this function is used for other thing, I can't change it, nvm.
I have this as result :
I just can't have the image ...
Caused by: java.lang.NullPointerExcep tion
at javax.swing.ImageIcon.<ini t>(Unknown Source)
at client.ui.images.Images.ge tImage(Ima ges.java:4 6)
at client.ui.images.Images.ne wImageIcon (Images.ja va:42)
at client.ui.images.Images.<c linit>(Ima ges.java:2 0)
public static Image getImage(String path){
return new ImageIcon(Images.class.get Resource(p ath)).getI mage();
}
I post a bit more code about the class :
public abstract class Images {
public static final ImageIcon VERY_HAPPY = newImageIcon("client/ui/im ages/smile ys/veryhap py.gif");
public static final ImageIcon HAPPY = newImageIcon("client/ui/im ages/smile ys/happy.g if");
public static final ImageIcon SAD = newImageIcon("client/ui/im ages/smile ys/sad.gif ");
public static final ImageIcon WINK = newImageIcon("client/ui/im ages/smile ys/wink.gi f");
public static final ImageIcon PROFIL = newImageIcon("client/ui/im ages/navba r/write.gi f");
public static final ImageIcon TCHAT = newImageIcon("client/ui/im ages/navba r/home.gif ");
public static final ImageIcon SALONS = newImageIcon("client/ui/im ages/navba r/folder_u p.gif");
public static final ImageIcon OPTIONS = newImageIcon("client/ui/im ages/navba r/write.gi f");
//TODO mieux gerer !!! retravailler ce truc
public static final ImageIcon IGNORE = newImageIcon("client/ui/im ages/other /cross.gif ");
public static final ImageIcon IGNORE2 = newImageIcon("client/ui/im ages/other /cross2.gi f");
public static final ImageIcon GHOST = newImageIcon("client/ui/im ages/other /cross2.gi f");
public static final ImageIcon IGNOREYOU = newImageIcon("client/ui/im ages/other /cross2.gi f");
public static final ImageIcon IG_IGYOU_GHOST = merge(getImage("client/ui/ images/oth er/cross.g if"), getImage("client/ui/images /other/cro ss2.gif")) ;
public static final ImageIcon IG_IGYOU = merge(getImage("client/ui/ images/oth er/cross.g if"), getImage("client/ui/images /other/cro ss2.gif")) ;
public static final ImageIcon IG_GHOST = merge(getImage("client/ui/ images/oth er/cross.g if"), getImage("client/ui/images /other/cro ss2.gif")) ;
public static final ImageIcon IGYOU_GHOST = merge(getImage("client/ui/ images/oth er/cross.g if"), getImage("client/ui/images /other/cro ss2.gif")) ;
public static ImageIcon newImageIcon(String path){
return new ImageIcon( getImage(path) );
}
public static Image getImage(String path){
return new ImageIcon(Images.class.get Resource(p ath)).getI mage();
}
....
}
And again, without the jar, in eclipse, I can see the images,
But when I make a jar for the applet, I have the errors above.
Yes I know, but this function is used for other thing, I can't change it, nvm.
I have this as result :
I just can't have the image ...
Caused by: java.lang.NullPointerExcep
at javax.swing.ImageIcon.<ini
at client.ui.images.Images.ge
at client.ui.images.Images.ne
at client.ui.images.Images.<c
public static Image getImage(String path){
return new ImageIcon(Images.class.get
}
I post a bit more code about the class :
public abstract class Images {
public static final ImageIcon VERY_HAPPY = newImageIcon("client/ui/im
public static final ImageIcon HAPPY = newImageIcon("client/ui/im
public static final ImageIcon SAD = newImageIcon("client/ui/im
public static final ImageIcon WINK = newImageIcon("client/ui/im
public static final ImageIcon PROFIL = newImageIcon("client/ui/im
public static final ImageIcon TCHAT = newImageIcon("client/ui/im
public static final ImageIcon SALONS = newImageIcon("client/ui/im
public static final ImageIcon OPTIONS = newImageIcon("client/ui/im
//TODO mieux gerer !!! retravailler ce truc
public static final ImageIcon IGNORE = newImageIcon("client/ui/im
public static final ImageIcon IGNORE2 = newImageIcon("client/ui/im
public static final ImageIcon GHOST = newImageIcon("client/ui/im
public static final ImageIcon IGNOREYOU = newImageIcon("client/ui/im
public static final ImageIcon IG_IGYOU_GHOST = merge(getImage("client/ui/
public static final ImageIcon IG_IGYOU = merge(getImage("client/ui/
public static final ImageIcon IG_GHOST = merge(getImage("client/ui/
public static final ImageIcon IGYOU_GHOST = merge(getImage("client/ui/
public static ImageIcon newImageIcon(String path){
return new ImageIcon( getImage(path) );
}
public static Image getImage(String path){
return new ImageIcon(Images.class.get
}
....
}
And again, without the jar, in eclipse, I can see the images,
But when I make a jar for the applet, I have the errors above.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>objects
the first image is in
client/ui/images/navbar/wr ite.gif
class Images has the following package declaration:
package client.ui.images;
So I put the path like this in class Images :
public static final ImageIcon PROFIL = newImageIcon("navbar/write .gif");
But it stills gives me an error when I want to access this image.
>> CEHJ
>> If you're just getting an Image, you may as well use ImageIO
Then I need to change everything to BufferedImage in my code ... :(
the first image is in
client/ui/images/navbar/wr
class Images has the following package declaration:
package client.ui.images;
So I put the path like this in class Images :
public static final ImageIcon PROFIL = newImageIcon("navbar/write
But it stills gives me an error when I want to access this image.
>> CEHJ
>> If you're just getting an Image, you may as well use ImageIO
Then I need to change everything to BufferedImage in my code ... :(
and you've verified that the images are in the jar
and that you aren't loading the classes from somewhere other than the jar
and that you aren't loading the classes from somewhere other than the jar
ASKER
I can see class Images in the jar in :
Client.jar\client\ui\image s
And the images in I can see class Images in the jar in :
Client.jar\client\ui\image s\navbar
Client.jar\client\ui\image
And the images in I can see class Images in the jar in :
Client.jar\client\ui\image
ASKER
oops type error
And the images in :
Client.jar\client\ui\image s\navbar
And the images in :
Client.jar\client\ui\image
and the Images class is not visible anywhere else?
ie. if you remove the Images class from the jar then it would fail to load
ie. if you remove the Images class from the jar then it would fail to load
ASKER
Yes, then I have :
java.lang.NoClassDefFoundE rror: client/ui/images/Images
java.lang.NoClassDefFoundE
>>Then I need to change everything to BufferedImage in my code ... :(
No. BufferedImage *is an* Image
No. BufferedImage *is an* Image
Please post the result of
jar tf whateveryourjaris.jar
jar tf whateveryourjaris.jar
ASKER
>> CEHJ
I have this error with ImageIO
Caused by: java.lang.IllegalArgumentE
at javax.imageio.ImageIO.read
at client.ui.images.Images.ge
at client.ui.images.Images.ne
at client.ui.images.Images.<c
Here is the result of the jar :
java.lang.ExceptionInIniti
at client.ui.AnimationPane.cr
at client.ui.AnimationPane.<i
at client.ui.ClientApplet.ini
at sun.applet.AppletPanel.run
at java.lang.Thread.run(Unkno
Caused by: java.lang.IllegalArgumentE
at javax.imageio.ImageIO.read
at client.ui.images.Images.ge
at client.ui.images.Images.ne
at client.ui.images.Images.<c
... 5 more
Exception in thread "thread applet-client/ui/ClientApp
at sun.plugin.util.GrayBoxPai
at sun.plugin.AppletViewer.sh
at sun.applet.AppletPanel.run
at java.lang.Thread.run(Unkno
> I have this error with ImageIO
usiung ImageIO is not going to make any difference.
Problem is nothing to do with loading the images, it is with finding them.
usiung ImageIO is not going to make any difference.
Problem is nothing to do with loading the images, it is with finding them.
ASKER
Ok I have no problems anymore :
I had a problem when getting the "PROFILE" image
public static final ImageIcon PROFIL = newImageIcon("navbar/write .gif");
So I tried to solve that problem,
without putting the correct paths to the other images
(because they were relative to the project and not the class).
And since I put the correct paths to the other images, everythinbg works now ...
I don't why it had put an error on PROFILE image since I corrected that one ....
weird error.
Anyway thx !
I had a problem when getting the "PROFILE" image
public static final ImageIcon PROFIL = newImageIcon("navbar/write
So I tried to solve that problem,
without putting the correct paths to the other images
(because they were relative to the project and not the class).
And since I put the correct paths to the other images, everythinbg works now ...
I don't why it had put an error on PROFILE image since I corrected that one ....
weird error.
Anyway thx !
:-)
getImage(new URL(getCodeBase(), "/images/x.jpg"));
with the correct path