Link to home
Start Free TrialLog in
Avatar of matthew016
matthew016Flag for Belgium

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.class"
        archive="Client.jar"
      name="Le Hall"
       ...

I have this exception :

java.lang.NullPointerException
      at javax.swing.ImageIcon.<init>(Unknown Source)
      at client.ui.AnimationPane.créerActionsSecToolbarContacts(AnimationPane.java:95)
        ...

I have the error at this line :

Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource(path));

The path is correct
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Use

getImage(new URL(getCodeBase(), "/images/x.jpg"));

with the correct path
Avatar of matthew016

ASKER

Thx, but I have an error with getCodeBase,
it is not in the JApplet class, (in a abstract class)
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)
Try something like

Image i = javax.imageio.ImageIO(getClass().getResourceAsStream("/images/x.jpg"));
make sure your image is in your jar, and use:

ImageIcon ii = new ImageIcon(getClass().getResource(path));

>>CEHJ
>>Image i = javax.imageio.ImageIO(getClass().getResourceAsStream("/images/x.jpg"));

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().getResource(path));

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(getClass().getResourceAsStream("/images/x.jpg"));

this doesn't compile
>>>>

Sorry

Image i = javax.imageio.ImageIO.read(getClass().getResourceAsStream("/images/x.jpg"));

> 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
I guess I can't use getClass because i'm in a static method
  public static Image getImage(String path){
          return javax.imageio.ImageIO.read(getClass().getResourceAsStream("/images/x.jpg"));   //error
    }
> 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.getResource(path)).getImage();
         // or return javax.imageio.ImageIO.read(MyClass.class.getResourceAsStream("/images/x.jpg"));
    }
>      at javax.swing.ImageIcon.<init>(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.getResource(path));
  }
> 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.NullPointerException
      at javax.swing.ImageIcon.<init>(Unknown Source)
      at client.ui.images.Images.getImage(Images.java:46)
      at client.ui.images.Images.newImageIcon(Images.java:42)
      at client.ui.images.Images.<clinit>(Images.java:20)


    public static Image getImage(String path){
          return new ImageIcon(Images.class.getResource(path)).getImage();
    }




I post a bit more code about the class :

public abstract class Images {
   
    public static final ImageIcon VERY_HAPPY = newImageIcon("client/ui/images/smileys/veryhappy.gif");
    public static final ImageIcon HAPPY = newImageIcon("client/ui/images/smileys/happy.gif");
    public static final ImageIcon SAD = newImageIcon("client/ui/images/smileys/sad.gif");
    public static final ImageIcon WINK = newImageIcon("client/ui/images/smileys/wink.gif");
   
    public static final ImageIcon PROFIL = newImageIcon("client/ui/images/navbar/write.gif");
    public static final ImageIcon TCHAT = newImageIcon("client/ui/images/navbar/home.gif");
    public static final ImageIcon SALONS = newImageIcon("client/ui/images/navbar/folder_up.gif");
    public static final ImageIcon OPTIONS = newImageIcon("client/ui/images/navbar/write.gif");
   
    //TODO mieux gerer !!! retravailler ce truc
    public static final ImageIcon IGNORE = newImageIcon("client/ui/images/other/cross.gif");
    public static final ImageIcon IGNORE2 = newImageIcon("client/ui/images/other/cross2.gif");
    public static final ImageIcon GHOST = newImageIcon("client/ui/images/other/cross2.gif");
    public static final ImageIcon IGNOREYOU = newImageIcon("client/ui/images/other/cross2.gif");
    public static final ImageIcon IG_IGYOU_GHOST = merge(getImage("client/ui/images/other/cross.gif"), getImage("client/ui/images/other/cross2.gif"));
    public static final ImageIcon IG_IGYOU = merge(getImage("client/ui/images/other/cross.gif"), getImage("client/ui/images/other/cross2.gif"));
    public static final ImageIcon IG_GHOST = merge(getImage("client/ui/images/other/cross.gif"), getImage("client/ui/images/other/cross2.gif"));
    public static final ImageIcon IGYOU_GHOST = merge(getImage("client/ui/images/other/cross.gif"), getImage("client/ui/images/other/cross2.gif"));
   
   
    public static ImageIcon newImageIcon(String path){
        return new ImageIcon( getImage(path) );
    }
   
    public static Image getImage(String path){
          return new ImageIcon(Images.class.getResource(path)).getImage();
    }

   ....

}
          



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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>objects

the first image is in
client/ui/images/navbar/write.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 ... :(
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
I can see class Images in the jar in :
Client.jar\client\ui\images


And the images in I can see class Images in the jar in :
Client.jar\client\ui\images\navbar

oops type error

And the images in :
Client.jar\client\ui\images\navbar
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
Yes, then I have :

java.lang.NoClassDefFoundError: client/ui/images/Images
>>Then I need to change everything to BufferedImage in my code ... :(

No. BufferedImage *is an* Image
Please post the result of

jar tf  whateveryourjaris.jar

>> CEHJ
I have this error with ImageIO
Caused by: java.lang.IllegalArgumentException: input == null!
      at javax.imageio.ImageIO.read(Unknown Source)
      at client.ui.images.Images.getImage(Images.java:50)
      at client.ui.images.Images.newImageIcon(Images.java:44)
      at client.ui.images.Images.<clinit>(Images.java:22)


Here is the result of the jar :


java.lang.ExceptionInInitializerError
      at client.ui.AnimationPane.créerActionsSecToolbarContacts(AnimationPane.java:97)
      at client.ui.AnimationPane.<init>(AnimationPane.java:50)
      at client.ui.ClientApplet.init(ClientApplet.java:108)
      at sun.applet.AppletPanel.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: input == null!
      at javax.imageio.ImageIO.read(Unknown Source)
      at client.ui.images.Images.getImage(Images.java:50)
      at client.ui.images.Images.newImageIcon(Images.java:44)
      at client.ui.images.Images.<clinit>(Images.java:22)
      ... 5 more
Exception in thread "thread applet-client/ui/ClientApplet.class" java.lang.NullPointerException
      at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
      at sun.plugin.AppletViewer.showAppletException(Unknown Source)
      at sun.applet.AppletPanel.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
> 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.
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 !
:-)