2

I am having big trouble to load a simple .gif image with the following code

   package etc_ejmp;
/*
 * Fig 21.2 AnimadorLogoJPanel.java
 * Animacion de una serie de imagenes*/
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import javax.swing.ImageIcon; 
import javax.swing.JPanel;
import javax.swing.Timer;

public class FIG21_2_AnimadorLogoJPanel extends JPanel{
    private final static String NOMBRE_IMAGEN ="deitel";// nombre de la imagen
    protected ImageIcon imagenes[];// arreglo de imagenes
    private final int TOTAL_IMAGENES=30;// numero de imagenes
    private int imagenActual =0;// indice de la imagen actual
    private final int RETRASO_ANIMACION=50;// retraso en milisegundos
    private int anchura;
    private int altura;

    private Timer temporizadosAnimacion;

    public FIG21_2_AnimadorLogoJPanel(){
        imagenes = new ImageIcon[TOTAL_IMAGENES];

        // carga 30 imagenes
        for(int cuenta=0;cuenta<imagenes.length; cuenta++){
            imagenes[ cuenta ] = new ImageIcon( getClass().getResource(
                      NOMBRE_IMAGEN + cuenta + ".gif" ) );
        }   


        anchura=imagenes[0].getIconWidth();
        altura=imagenes[0].getIconHeight();
    }
}

I have tried with

imagenes[ cuenta ] = new ImageIcon( getClass().getResource("deitel0.gif" ) ); // nothing
imagenes[ cuenta ] = new ImageIcon( getClass().getResource("deitel.gif" ) ); // nothing

but it only works with .png. Also if i try to use numbers inside the string, for example "hello01.gif/.png" --ERROR

the error is;

Exception in thread "main" java.lang.NullPointerException   at
 javax.swing.ImageIcon.<init>(Unknown Source)   at
 etc_ejmp.FIG21_2_AnimadorLogoJPanel.<init>(FIG21_2_AnimadorLogoJPanel.java:29) at
 com.ejercDietel.llamada.llamada_cap21_ejemplos.FIG21_2_AnimadorLogoJPanel(llamada_cap21_ejemplos.java:13) at 
 com.ejercDietel.llamada.EXECAP21.<init>(EXECAP21.java:9)   at
 llamadas.CallCap21(llamadas.java:222)  at
 InicioSistemaPruebas.main(InicioSistemaPruebas.java:67)

I don't know what I am missing, it is like the compiler doesn't see the images with .gif format also with number in the file-name.

The name of the images are

deitel0.gif
deitel1.gif
deitel10.gif
deitel11.gif
deitel12.gif
deitel13.gif
deitel14.gif
deitel15.gif
deitel16.gif
deitel17.gif
deitel18.gif
deitel19.gif
deitel2.gif
deitel20.gif
deitel21.gif
deitel22.gif
deitel23.gif
deitel24.gif
deitel25.gif
deitel26.gif
deitel27.gif
deitel28.gif
deitel29.gif
deitel3.gif
deitel4.gif
deitel5.gif
deitel6.gif
deitel7.gif
deitel8.gif
deitel9.gif
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    *"it is like the compiler doesn't see the images"* The compiler never looks for the images. It is the run-time that must be producing the message. – Andrew Thompson Aug 30 '14 at 02:03
  • 1
    What are the actual names of the image files? I have to wonder if you need to do some 0 padding of your String. – Hovercraft Full Of Eels Aug 30 '14 at 02:03
  • 1
    1) BTW: By the time of deployment these will likely become an [tag:embedded-resource] that must be accessed by `URL` rather than `File`. 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Aug 30 '14 at 02:06
  • @AndrewThompson yes i know is a kind of joking way to say it xD. – Douglas Leekam Aug 30 '14 at 02:08
  • *"i know is a kind of way to say it"* What are you referring to? – Andrew Thompson Aug 30 '14 at 02:09
  • 1
    Where are the images located? By using just the file name as the relative path, you're saying the images are in the same package as class calling it. If you have them at the root of the classpath the you need a leading `/` in front of the file name. – Paul Samsotha Aug 30 '14 at 02:24
  • @AndrewThompson i was refering to a "way to say something" jejeje sorry – Douglas Leekam Aug 30 '14 at 02:27
  • @peeskillet something like this? "/"+NOMBRE_IMAGEN + cuenta + ".gif" – Douglas Leekam Aug 30 '14 at 02:27
  • Why don't you answer the first part of the question first. Then I can answer yours – Paul Samsotha Aug 30 '14 at 02:28
  • I assume that the images are from Deitel's How to Program Java, 9th edition. If so, what chapter? And where are the images relative to your class files? – Hovercraft Full Of Eels Aug 30 '14 at 02:34
  • @HovercraftFullOfEels yup they are from that book, chapter 21 FIG21.2, by now the images are in the classpath, but i tried with both classpath and inside a filefolder but the result is the same. – Douglas Leekam Aug 30 '14 at 02:41
  • @peeskillet At the moment that i read your comment, I tried with the "\" but the result is the same – Douglas Leekam Aug 30 '14 at 02:43
  • 1
    Please answer `Where are the images located?` in the form of something like `src/my/images/are/here`. We need to know exactly where your images are. – Paul Samsotha Aug 30 '14 at 02:44
  • Sorry hehe @peeskillet src/(images are here) ******** src/file.java src/alltheimages – Douglas Leekam Aug 30 '14 at 02:48
  • If your images are `src/image.png` the you should use the path `/image.png` – Paul Samsotha Aug 30 '14 at 02:49
  • @peeskillet Something like this?; `imagenes[ cuenta ] = new ImageIcon( getClass().getResource("/deitel0.gif" ) );` – Douglas Leekam Aug 30 '14 at 02:53
  • @peeskillet yes same error. – Douglas Leekam Aug 30 '14 at 02:54
  • 2
    @DouglasLeekam: Please have a look at how to [add Images to Java Project](http://stackoverflow.com/a/9866659/1057230), though this link has already been provided, inside [__info__](http://stackoverflow.com/tags/embedded-resource/info) of [tag:embedded-resource], before. I hope it helps. – nIcE cOw Aug 30 '14 at 03:04
  • 2
    If you haven't already solved the problem, consider posting a [simple runnable example](http://stackoverflow.com/help/mcve) that we can copy-paste-compile-run that demonstrates the problem. The path seems to be correct (i.e. /image.png given your image is in src/image.png) so the problem may be elsewhere. – Paul Samsotha Aug 30 '14 at 04:04

3 Answers3

0

Try using this link.

It Appears to have a few good answer's on gif's with image icon.

Hope this helps

EDIT: Are you sure your image is in the correct path? or the path is correct, it looks as though this is the problem

for(int cuenta=0;cuenta<imagenes.length; cuenta++){
        imagenes[ cuenta ] = new ImageIcon( getClass().getResource(
                  NOMBRE_IMAGEN + cuenta + ".gif" ) );
    }

Is your image in your class path or in another directory, if its in your jar, then make sure you use the full path, eg com\\myprogram\\image01.gif

Community
  • 1
  • 1
Luke Melaia
  • 1,470
  • 14
  • 22
0

In my opinion, it looks clearly that a requested image(file) was not found. check if all files really exist.
Also check the case-sensitivity of the file names.
maybe there is something like deitel22 .GIF

Ben
  • 3,378
  • 30
  • 46
0

It looks like Java does not like .gif extension, it is not the type of file but just the extension. I tested using .png files and works fine. I just copied and rename the same files, same directory from .gif to .png and run the applications... what do you think, it worked!