Search code examples
javaimageclassloadergetresource

Java: NullpointerException by


I get a NullPointerException while running the code below. I don not know why I get it and it keeps me from getting any further in development.

MyImage.java:

import java.awt.Image;
import java.awt.Toolkit;

public class MyImage {

    private String name;
    private Image img = null;

    public MyImage(String n) {
        this.name=n;
    }

    public Image get() {
        if (img == null) {
            img = Toolkit.getDefaultToolkit()
                         .createImage(getClass()
                         .getClassLoader()
                         .getResource(name));
        }
        return (img);
    }
}

PaintableImage.java:

package name.burre.jan.animationGame;

import java.awt.Graphics;
import javax.swing.JPanel;

public class PaintableImage extends JPanel implements Paintable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    MyImage mi;

    public PaintableImage (MyImage m) {
        this.mi = m;
    }

    public PaintableImage(String name) {
        this.mi = new MyImage(name);
    }

    @Override
    public void paintTo(Graphics g) {
        g.drawImage(mi.get(),0,0,this);
    }

    public static void main (String[] args) {
        PaintableImage pi = new PaintableImage("img/Flower.jpg");
        System.out.println(pi.mi.get().getHeight(pi));
    }
}

My MyImage.class and my PaintableImage.class ly in one folder and in this folder lies an img-folder where my Flower.jpg is located. Any suggestions?


Solution

  • Null pointer Error you are facing should be at this line

    img = Toolkit.getDefaultToolkit()
                             .createImage(getClass()
                             .getClassLoader()
                             .getResource(name));
    

    This is because it could not find the file Flower.jpg. When u give getClassLoader().getResource(name), it looks for the img folder in root directory where class files are located.

    You can see the below example to understand better

    Source

    package Sound;
    public class ResourceTest {
        public static void main(String[] args) {
            String fileName = "Kalimba.mp3";
            System.out.println(fileName);
            System.out.println(new ResourceTest().getClass().getResource(fileName));
            System.out.println(new ResourceTest().getClass().getClassLoader().getResource(fileName));
    

    Output

    Kalimba.mp3
    file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Sound/Kalimba.mp3
    file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Kalimba.mp3
        }
    }
    

    Got example from here - Java - getClassLoader().getResource() driving me bonkers

    Hope it helps.

    Move your image folder to root directory of where the class files are located, to locate that - can u do a print like above.

    Example - if its a web based project, classes would go under web-inf/classes, so you should put your img folder inside classes folder.