Avatar of matthew016
matthew016
Flag 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
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

Use

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

with the correct path
matthew016

ASKER
Thx, but I have an error with getCodeBase,
it is not in the JApplet class, (in a abstract class)
CEHJ

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
matthew016

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)
CEHJ

Try something like

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

make sure your image is in your jar, and use:

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
matthew016

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

>>>>
>>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"));
Mick Barry


> 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
Your help has saved me hundreds of hours of internet surfing.
fblack61
matthew016

ASKER
I guess I can't use getClass because i'm in a static method
matthew016

ASKER
  public static Image getImage(String path){
          return javax.imageio.ImageIO.read(getClass().getResourceAsStream("/images/x.jpg"));   //error
    }
Mick Barry

> 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"));
    }
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mick Barry

>      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));
  }
matthew016

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.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
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
matthew016

ASKER
>>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 ... :(
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Mick Barry

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
matthew016

ASKER
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

matthew016

ASKER
oops type error

And the images in :
Client.jar\client\ui\images\navbar
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mick Barry

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
matthew016

ASKER
Yes, then I have :

java.lang.NoClassDefFoundError: client/ui/images/Images
CEHJ

>>Then I need to change everything to BufferedImage in my code ... :(

No. BufferedImage *is an* Image
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
CEHJ

Please post the result of

jar tf  whateveryourjaris.jar
matthew016

ASKER

>> 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)
Mick Barry

> 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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
matthew016

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 !
CEHJ

:-)