0

I'm having a NullPointerException when I compile my code but it works fine in eclipse.

Error log from command prompt:

java.lang.NullPointerException
        at sun.awt.image.URLImageSource.getConnection(Unknown Source)
        at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
        at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
        at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
        at sun.awt.image.ImageFetcher.run(Unknown Source)

The class getting the images is:

package tileGame;

import java.awt.Toolkit;
import javax.swing.ImageIcon;


public class Image {


public String address1;
public String address2;
public String name;
public String type;

public java.awt.Image image;

public Image(String name){
    address1 = System.getProperty("user.dir");
    address2 = "/Resources/Images/";
    type = "png";
    this.name = name;
}
public Image(String name, String type){
    address1 = System.getProperty("user.dir");
    address2 = "/src/Resources/Images/";
    this.type = type;
    this.name = name;
}
public Image(String name, String type, String address){
    address1 = System.getProperty("user.dir");
    this.address2 = address;
    this.type = type;
    this.name = name;
}
public void loadImage(){
    //image = new ImageIcon(address1 + address2 + name + "." + type).getImage();
    image = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource(address2 + name + "." + type))).getImage();
    System.out.println("Image Loaded '" + name + "' at '" + address2 + name + "." + type + "'");
}

}

There is probably a good reason for this that I can't see as I am fairly new to Java so if anyone could give me some pointers then that would be welcome.

Edit: OK I fixed it just common human error that I wouldn't have spotted on my own. Thanks.

Croccy21
  • 13
  • 6

3 Answers3

3

getClass().getResource(path) accesses a resource "file" in the class path (read-only). If the application is compiled, this file resides inside the jar. With 7zip or WinZip you might look inside the jar and find the path.

The path uses as any URL / as path separator. The path can be relative to the class (remember getClass()), that is the package path. The path can also be absolute to the jar's root: "/...".

Furthermore the path must be case-sensitive, not as Windows file paths.

In your case no need for any system property to get a file system path.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • OK my bad it turns out that it was the case sensitive thing, I had changed the names of the files since the creation of the class and not fixed it as it still worked in eclipse so I didn't fix it. Thanks for the help. – Croccy21 Jun 14 '13 at 19:14
0

This is because of relative-path issue. java.exe is starting from different directories in eclipse and command prompt.

To make sure that it works:

  1. Use absolute path (to confirm the issue)
  2. When running the program use -cp and add the necessary directories to classpath.
Chris
  • 5,584
  • 9
  • 40
  • 58
0

This class has no compilation failures (apart from the missing final })

What you are having is a problem when running your code. You are probably having the same issue described here and it is probably in the getClass().getResource(address2 + name + "." + type).

Reading resources from the classpath is tricky because you don get exceptions when you dont find them, you instead get null. Also when reading from the classpath you have to be careful if you are using a class or a classloader as paths change. More details here

Community
  • 1
  • 1
Bruno Penteado
  • 2,234
  • 2
  • 23
  • 26