-2

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?

Turing85
  • 18,217
  • 7
  • 33
  • 58
BlackFool
  • 61
  • 4
  • 7
    Please provide the whole exception message. – Turing85 Jun 25 '15 at 20:12
  • The stack trace would be very useful. – rodit Jun 25 '15 at 20:19
  • My guess without a stacktrace is that it doesnt find the image at `img/Flower.jpg`. This then causes your `get()` to return null (or possibly throw the NPE itself), which then causes `g.drawImage()` to throw the NPE. But a stacktrace would really help. – user489041 Jun 25 '15 at 20:19

1 Answers1

1

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.

Community
  • 1
  • 1
Srikanth Balaji
  • 2,638
  • 4
  • 18
  • 31
  • 1
    Mark as answered(Tick) if it gets solved with the above inputs – Srikanth Balaji Jun 25 '15 at 20:50
  • Thank you. My problem really was the location of my img-Folder. The problem I faced, that the book I'm learning from didn't explain the exact meaning of root-folder of my classes. So thanks to your example I can go on now. : ) – BlackFool Jun 28 '15 at 10:22
  • Nice to hear, But from next time - Detail out the error with trace. So you can get to solve the problem much faster. – Srikanth Balaji Jun 29 '15 at 06:59