confusing

First I want to say hey to all :slight_smile:

I have been programming with java about a year now.
I also study computer science in university of Jyvaskyla in Finland and I’m starting my second year.

That’s for me for now and to my problem which has been bothering me.

Here is working piece of code:


Image source = null;

URL ur l= this.getClass().getClassLoader().getResource(ref);
if(url == null) return null;
try {
source = ImageIO.read(url);
}
catch (IOException ex) {
}


ImageIO.read(url);


this returns BufferedImage type object…

but the piece of code what confuses me alot


BufferedImage sourceImage = null;

try {
URL url = this.getClass().getClassLoader().getResource(ref);

     if (url == null) {
           return null;
     }

      // use ImageIO to read the image in
      System.out.println(url); // this prints the rigth path
                   
     sourceImage = ImageIO.read(url);
 } catch (IOException e) {
       
 }
 System.out.println(sourceImage); // this prints null!! ???

and here I have used “correctly” ImageIO.read(url) -method…

Thanks Kev Glass for the great tutorial of SpaceInvaders :slight_smile:

T: Juenvalj

Maybe you get an exception in your empty try/catch block?
Have you tried to see if you get a exception in that block? like:

 
} catch (IOException e) {
e.printStackTrace();
}

to see what is going on?

// Gregof

yes I put it there but the line ImageIO.read doesn’t make exception there… it just prints null… like it doesn’t now how to read that file…

Reading the javadoc:

http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/ImageIO.html#read(java.net.URL)

So null we be returned if you’re trying to read an image for a while an ImageReader doesn’t exist. What type of image is it you’re trying to load?

Kev

I found the problem…

I used ms paint to make the images :-[ sorry never happens again I promise! So I did those images again with photoshop and it worked beatifully.

those images are .gif